1: <?php
2: /**
3: * Console Logging
4: *
5: * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
6: * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7: *
8: * Licensed under The MIT License
9: * For full copyright and license information, please see the LICENSE.txt
10: * Redistributions of files must retain the above copyright notice.
11: *
12: * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13: * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
14: * @package Cake.Log.Engine
15: * @since CakePHP(tm) v 2.2
16: * @license http://www.opensource.org/licenses/mit-license.php MIT License
17: */
18:
19: App::uses('BaseLog', 'Log/Engine');
20: App::uses('ConsoleOutput', 'Console');
21:
22: /**
23: * Console logging. Writes logs to console output.
24: *
25: * @package Cake.Log.Engine
26: */
27: class ConsoleLog extends BaseLog {
28:
29: /**
30: * Output stream
31: *
32: * @var ConsoleOutput
33: */
34: protected $_output = null;
35:
36: /**
37: * Constructs a new Console Logger.
38: *
39: * Config
40: *
41: * - `types` string or array, levels the engine is interested in
42: * - `scopes` string or array, scopes the engine is interested in
43: * - `stream` the path to save logs on.
44: * - `outputAs` integer or ConsoleOutput::[RAW|PLAIN|COLOR]
45: *
46: * @param array $config Options for the FileLog, see above.
47: * @throws CakeLogException
48: */
49: public function __construct($config = array()) {
50: parent::__construct($config);
51: if (DS === '\\' && !(bool)env('ANSICON')) {
52: $outputAs = ConsoleOutput::PLAIN;
53: } else {
54: $outputAs = ConsoleOutput::COLOR;
55: }
56: $config = Hash::merge(array(
57: 'stream' => 'php://stderr',
58: 'types' => null,
59: 'scopes' => array(),
60: 'outputAs' => $outputAs,
61: ), $this->_config);
62: $config = $this->config($config);
63: if ($config['stream'] instanceof ConsoleOutput) {
64: $this->_output = $config['stream'];
65: } elseif (is_string($config['stream'])) {
66: $this->_output = new ConsoleOutput($config['stream']);
67: } else {
68: throw new CakeLogException('`stream` not a ConsoleOutput nor string');
69: }
70: $this->_output->outputAs($config['outputAs']);
71: }
72:
73: /**
74: * Implements writing to console.
75: *
76: * @param string $type The type of log you are making.
77: * @param string $message The message you want to log.
78: * @return boolean success of write.
79: */
80: public function write($type, $message) {
81: $output = date('Y-m-d H:i:s') . ' ' . ucfirst($type) . ': ' . $message . "\n";
82: return $this->_output->write(sprintf('<%s>%s</%s>', $type, $output, $type), false);
83: }
84:
85: }
86: