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