|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of EC-CUBE |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved. |
|
6
|
|
|
* |
|
7
|
|
|
* http://www.lockon.co.jp/ |
|
8
|
|
|
* |
|
9
|
|
|
* This program is free software; you can redistribute it and/or |
|
10
|
|
|
* modify it under the terms of the GNU General Public License |
|
11
|
|
|
* as published by the Free Software Foundation; either version 2 |
|
12
|
|
|
* of the License, or (at your option) any later version. |
|
13
|
|
|
* |
|
14
|
|
|
* This program is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU General Public License |
|
20
|
|
|
* along with this program; if not, write to the Free Software |
|
21
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
namespace Eccube\Monolog\Helper; |
|
25
|
|
|
|
|
26
|
|
|
use Eccube\Application; |
|
27
|
|
|
use Eccube\Entity\Customer; |
|
28
|
|
|
use Eccube\Entity\Member; |
|
29
|
|
|
use Eccube\Monolog\Processor\EccubeWebProcessor; |
|
30
|
|
|
use Monolog\Formatter\LineFormatter; |
|
31
|
|
|
use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy; |
|
32
|
|
|
use Monolog\Handler\FingersCrossedHandler; |
|
33
|
|
|
use Monolog\Handler\RotatingFileHandler; |
|
34
|
|
|
use Monolog\Logger; |
|
35
|
|
|
use Monolog\Processor\IntrospectionProcessor; |
|
36
|
|
|
use Monolog\Processor\UidProcessor; |
|
37
|
|
|
|
|
38
|
|
|
class EccubeMonologHelper |
|
|
|
|
|
|
39
|
|
|
{ |
|
40
|
|
|
|
|
41
|
|
|
/** @var \Eccube\Application */ |
|
42
|
|
|
protected $app; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
|
|
|
|
|
45
|
|
|
* EccubeMonologHelper constructor. |
|
46
|
|
|
* |
|
47
|
|
|
*/ |
|
48
|
1061 |
|
public function __construct($app) |
|
49
|
|
|
{ |
|
50
|
1061 |
|
$this->app = $app; |
|
51
|
1061 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* log.ymlの内容に応じたHandlerの設定を行う |
|
55
|
|
|
* |
|
56
|
|
|
* @param array $channelValues |
|
57
|
|
|
* @return FingersCrossedHandler |
|
58
|
|
|
*/ |
|
59
|
1062 |
|
public function getHandler(array $channelValues) |
|
60
|
|
|
{ |
|
61
|
1061 |
|
$app = $this->app; |
|
62
|
|
|
|
|
63
|
1061 |
|
$levels = Logger::getLevels(); |
|
64
|
|
|
|
|
65
|
|
|
// ファイル名などの設定を行い、設定がなければデフォルト値を設定 |
|
66
|
1061 |
|
$logFileName = isset($channelValues['filename']) ? $channelValues['filename'] : $app['config']['log']['filename']; |
|
67
|
1061 |
|
$delimiter = isset($channelValues['delimiter']) ? $channelValues['delimiter'] : $app['config']['log']['delimiter']; |
|
68
|
1061 |
|
$dateFormat = isset($channelValues['dateformat']) ? $channelValues['dateformat'] : $app['config']['log']['dateformat']; |
|
69
|
1061 |
|
$logLevel = isset($channelValues['log_level']) ? $channelValues['log_level'] : $app['config']['log']['log_level']; |
|
70
|
1061 |
|
$actionLevel = isset($channelValues['action_level']) ? $channelValues['action_level'] : $app['config']['log']['action_level']; |
|
71
|
1061 |
|
$maxFiles = isset($channelValues['max_files']) ? $channelValues['max_files'] : $app['config']['log']['max_files']; |
|
72
|
1061 |
|
$logDateFormat = isset($channelValues['log_dateformat']) ? $channelValues['log_dateformat'] : $app['config']['log']['log_dateformat']; |
|
73
|
1061 |
|
$logFormat = isset($channelValues['log_format']) ? $channelValues['log_format'] : $app['config']['log']['log_format']; |
|
74
|
|
|
|
|
75
|
1061 |
|
if ($app['debug']) { |
|
76
|
1054 |
|
$level = Logger::DEBUG; |
|
77
|
1054 |
|
} else { |
|
78
|
7 |
|
$level = $logLevel; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
// RotateHandlerの設定 |
|
83
|
1061 |
|
$logDir = isset($app['config']['log_realdir']) ? $app['config']['log_realdir'].'/' : $app['config']['root_dir'].'/app/log/'; |
|
84
|
1061 |
|
$filename = $logDir.$logFileName.'.log'; |
|
85
|
1061 |
|
$RotateHandler = new RotatingFileHandler($filename, $maxFiles, $level); |
|
86
|
1061 |
|
$RotateHandler->setFilenameFormat( |
|
87
|
1061 |
|
$logFileName.$delimiter.'{date}'.$app['config']['log']['suffix'], |
|
88
|
|
|
$dateFormat |
|
89
|
1061 |
|
); |
|
90
|
|
|
|
|
91
|
|
|
// ログフォーマットの設定(設定ファイルで定義) |
|
92
|
1061 |
|
$RotateHandler->setFormatter(new LineFormatter($logFormat.PHP_EOL, $logDateFormat, true, true)); |
|
93
|
|
|
|
|
94
|
|
|
// FingerCossedHandlerの設定 |
|
95
|
1061 |
|
$FingerCrossedHandler = new FingersCrossedHandler( |
|
96
|
1061 |
|
$RotateHandler, |
|
|
|
|
|
|
97
|
1061 |
|
new ErrorLevelActivationStrategy($levels[$actionLevel]) |
|
98
|
1061 |
|
); |
|
99
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
// Processorの内容をログ出力 |
|
102
|
1061 |
|
$webProcessor = new EccubeWebProcessor(); |
|
103
|
1061 |
|
$uidProcessor = new UidProcessor(8); |
|
104
|
|
|
|
|
105
|
1062 |
|
$FingerCrossedHandler->pushProcessor(function ($record) use ($app, $uidProcessor, $webProcessor) { |
|
106
|
|
|
// ログフォーマットに出力する値を独自に設定 |
|
107
|
|
|
|
|
108
|
1062 |
|
$record['level_name'] = sprintf("%-5s", $record['level_name']); |
|
109
|
|
|
|
|
110
|
|
|
// セッションIDと会員IDを設定 |
|
111
|
1062 |
|
$record['session_id'] = null; |
|
112
|
1062 |
|
$record['user_id'] = null; |
|
113
|
1062 |
|
if ($app->isBooted()) { |
|
114
|
1061 |
|
if (isset($app['session'])) { |
|
115
|
1058 |
|
$record['session_id'] = substr(sha1($app['session']->getId()), 0, 8); |
|
116
|
1058 |
|
} |
|
117
|
1061 |
|
$user = $app->user(); |
|
118
|
1061 |
|
if ($user instanceof Customer || $user instanceof Member) { |
|
|
|
|
|
|
119
|
381 |
|
$record['user_id'] = $user->getId(); |
|
120
|
381 |
|
} |
|
121
|
1061 |
|
} |
|
122
|
|
|
|
|
123
|
1062 |
|
$record['uid'] = $uidProcessor->getUid(); |
|
124
|
|
|
|
|
125
|
1062 |
|
$record['url'] = $webProcessor->getRequestUri(); |
|
126
|
1062 |
|
$record['ip'] = $webProcessor->getClientIp(); |
|
127
|
1062 |
|
$record['referrer'] = $webProcessor->getReferer(); |
|
128
|
1062 |
|
$record['method'] = $webProcessor->getMethod(); |
|
129
|
1062 |
|
$record['user_agent'] = $webProcessor->getUserAgent(); |
|
130
|
|
|
|
|
131
|
|
|
// クラス名などを一旦保持し、不要な情報は削除 |
|
132
|
1062 |
|
$line = $record['extra']['line']; |
|
133
|
1062 |
|
$functionName = $record['extra']['function']; |
|
134
|
|
|
// php5.3だとclass名が取得できないため、ファイル名を元に出力 |
|
135
|
|
|
// $className = $record['extra']['class']; |
|
|
|
|
|
|
136
|
1062 |
|
$className = $record['extra']['file']; |
|
137
|
|
|
|
|
138
|
|
|
// 不要な情報を削除 |
|
139
|
1062 |
|
unset($record['extra']['file']); |
|
140
|
1062 |
|
unset($record['extra']['line']); |
|
141
|
1062 |
|
unset($record['extra']['class']); |
|
142
|
1062 |
|
unset($record['extra']['function']); |
|
143
|
|
|
|
|
144
|
1062 |
|
$record['class'] = pathinfo($className, PATHINFO_FILENAME); |
|
145
|
1062 |
|
$record['function'] = $functionName; |
|
146
|
1062 |
|
$record['line'] = $line; |
|
147
|
|
|
|
|
148
|
1062 |
|
return $record; |
|
149
|
1061 |
|
}); |
|
150
|
|
|
|
|
151
|
|
|
// クラス名等を取得するProcessor、ログ出力時にクラス名を無視するための設定を行っている |
|
152
|
1061 |
|
$intro = new IntrospectionProcessor(Logger::DEBUG, array('Psr\\Log\\', 'EccubeLog')); |
|
153
|
1061 |
|
$FingerCrossedHandler->pushProcessor($intro); |
|
154
|
|
|
|
|
155
|
1061 |
|
return $FingerCrossedHandler; |
|
156
|
|
|
|
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
} |
|
160
|
|
|
|