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