log::note()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
// +----------------------------------------------------------------------
3
// | PHPSpider [ A PHP Framework For Crawler ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006-2014 https://doc.phpspider.org All rights reserved.
6
// +----------------------------------------------------------------------
7
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8
// +----------------------------------------------------------------------
9
// | Author: Seatle Yang <[email protected]>
10
// +----------------------------------------------------------------------
11
12
//----------------------------------
13
// PHPSpider日志类文件
14
//----------------------------------
15
16
namespace phpspider\core;
17
// 引入PATH_DATA
18
require_once __DIR__ . '/constants.php';
19
20
class log
21
{
22
    public static $log_show = false;
23
    public static $log_type = false;
24
    public static $log_file = "data/phpspider.log";
25
    public static $out_sta = "";
26
    public static $out_end = "";
27
28
    public static function note($msg)
29
    {
30
        self::$out_sta = self::$out_end = "";
31
        self::msg($msg, 'note');
32
    }
33
34
    public static function info($msg)
35
    {
36
        self::$out_sta = self::$out_end = "";
37
        self::msg($msg, 'info');
38
    }
39
40
    public static function warn($msg)
41
    {
42
        self::$out_sta = self::$out_end = "";
43
        if (!util::is_win()) 
44
        {
45
            self::$out_sta = "\033[33m";
46
            self::$out_end = "\033[0m";
47
        }
48
49
        self::msg($msg, 'warn');
50
    }
51
52
    public static function debug($msg)
53
    {
54
        self::$out_sta = self::$out_end = "";
55
        if (!util::is_win()) 
56
        {
57
            self::$out_sta = "\033[36m";
58
            self::$out_end = "\033[0m";
59
        }
60
61
        self::msg($msg, 'debug');
62
    }
63
64
    public static function error($msg)
65
    {
66
        self::$out_sta = self::$out_end = "";
67
        if (!util::is_win()) 
68
        {
69
            self::$out_sta = "\033[31m";
70
            self::$out_end = "\033[0m";
71
        }
72
73
        self::msg($msg, 'error');
74
    }
75
76
    public static function msg($msg, $log_type)
77
    {
78
        if ($log_type != 'note' && self::$log_type && strpos(self::$log_type, $log_type) === false) 
0 ignored issues
show
Bug introduced by
self::log_type of type true is incompatible with the type string expected by parameter $haystack of strpos(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

78
        if ($log_type != 'note' && self::$log_type && strpos(/** @scrutinizer ignore-type */ self::$log_type, $log_type) === false) 
Loading history...
79
        {
80
            return false;
81
        }
82
83
        if ($log_type == 'note') 
84
        {
85
            $msg = self::$out_sta. $msg . "\n".self::$out_end;
86
        }
87
        else 
88
        {
89
            $msg = self::$out_sta.date("Y-m-d H:i:s")." [{$log_type}] " . $msg .self::$out_end. "\n";
90
        }
91
        if(self::$log_show)
92
        {
93
            echo $msg;
94
        }
95
        file_put_contents(self::$log_file, $msg, FILE_APPEND | LOCK_EX);
96
    }
97
98
    /**
99
     * 记录日志 XXX
100
     * @param string $msg
101
     * @param string $log_type  Note|Warning|Error
102
     * @return void
103
     */
104
    public static function add($msg, $log_type = '')
105
    {
106
        if ($log_type != '') 
107
        {
108
            $msg = date("Y-m-d H:i:s")." [{$log_type}] " . $msg . "\n";
109
        }
110
        if(self::$log_show)
111
        {
112
            echo $msg;
113
        }
114
        //file_put_contents(PATH_DATA."/log/".strtolower($log_type).".log", $msg, FILE_APPEND | LOCK_EX);
115
        file_put_contents(PATH_DATA."/log/error.log", $msg, FILE_APPEND | LOCK_EX);
116
    }
117
118
}
119
120