統計
| ブランチ: | リビジョン:

pictcode / lib / Cake / Log / Engine / ConsoleLog.php @ 635eef61

履歴 | 表示 | アノテート | ダウンロード (2.559 KB)

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') && env('ConEmuANSI') !== 'ON') ||
52
                        (function_exists('posix_isatty') && !posix_isatty($this->_output))
53
                ) {
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 bool 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
}