-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileLogger.php
153 lines (131 loc) · 3.55 KB
/
FileLogger.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
/**
* AttwFramework
*
* @author Gabriel Jacinto <[email protected]>
* @license MIT License
* @link http://attwframework.github.io
*/
namespace Attw\Logger;
use Attw\Logger\AbstractLogger;
use \RuntimeException;
use \InvalidArgumentException;
use \DateTime;
use \DateTimeZone;
/**
* File logger
*/
class FileLogger extends AbstractLogger
{
/**
* File to registry the logs
*
* @var string
*/
private static $file;
/**
* Log to registry in file
*
* @var string
*/
private static $log;
/**
* Replace log content to the ultimate
*
* @var boolean false to append a new log
*/
private static $replace = false;
/**
* Locals to save logs of specific type
*
* @var array
*/
private static $locals = array();
/**
* Set where the log will be registred
*
* @param string $file
* if file doesn't exists, create empty
* @throws \RuntimeException case file not exists
* @throws \RuntimeException case file is not writeable
*/
public static function setFile($file)
{
if (!file_exists($file)) {
throw new RuntimeException('File not found: ' . $file);
}
if (!is_writable($file)) {
throw new RuntimeException('File not writeable: ' . $file);
}
self::$file = (string) $file;
}
/**
* Choose if the new log will replace the file to it
*
* @param boolean $replace
* @throws \InvalidAgumentException case param $replace is not boolean
*/
public static function replace($replace)
{
if (!is_bool($replace)) {
throw new InvalidArgumentException('The first argument must be boolean');
}
self::$replace = $replace;
}
/**
* Write a log in a file
*
* @param string $type
* @param string $message
* @return void
*/
public static function write($type, $message)
{
self::logConstruct($type, $message);
if (is_null(self::$file)) {
if (count(self::$locals) > 0) {
foreach (self::$locals as $logType => $local) {
if ($logType == $type) {
self::setFile($local);
}
}
if (is_null(self::$file)) {
throw new RuntimeException('Define a file to save the logs or a file to save the logs');
}
}
}
return (!self::$replace) ? file_put_contents(self::$file, self::$log . "\n", FILE_APPEND) :
file_put_contents(self::$file, self::$log . "\n");
}
/**
* Construct a log by the log structure
*
* @param string $type
* @param string $message
*/
private static function logConstruct($type, $message)
{
$types = array_flip(self::$types);
if (isset($types[ $type ])) {
$type = $types[ $type ];
}
$date = new DateTime();
$date = $date->format('Y/m/d H:i:s');
$arr = array_merge(self::$customizedCamps, array(
':type' => ucfirst($type), ':date' => $date, ':message' => $message
));
self::$log = self::$logStructure;
foreach ($arr as $camp => $value) {
self::$log = str_replace($camp, $value, self::$log);
}
}
/**
* Set local for specific types of logs
*
* @param array $locals
*/
public static function setLogsLocals(array $locals)
{
self::$locals = array_merge(self::$locals, $locals);
}
}