-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlog.php
99 lines (88 loc) · 2.68 KB
/
log.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
<?php namespace Laravel;
class Log {
/**
* Log an exception to the log file.
*
* @param Exception $e
* @return void
*/
public static function exception($e)
{
static::write('error', static::exception_line($e));
}
/**
* Format a log friendly message from the given exception.
*
* @param Exception $e
* @return string
*/
protected static function exception_line($e)
{
return $e->getMessage().' in '.$e->getFile().' on line '.$e->getLine();
}
/**
* Write a message to the log file.
*
* <code>
* // Write an "error" message to the log file
* Log::write('error', 'Something went horribly wrong!');
*
* // Write an "error" message using the class' magic method
* Log::error('Something went horribly wrong!');
*
* // Log an arrays data
* Log::write('info', array('name' => 'Sawny', 'passwd' => '1234', array(1337, 21, 0)), true);
* //Result: Array ( [name] => Sawny [passwd] => 1234 [0] => Array ( [0] => 1337 [1] => 21 [2] => 0 ) )
* //If we had omit the third parameter the result had been: Array
* </code>
*
* @param string $type
* @param string $message
* @return void
*/
public static function write($type, $message, $pretty_print = false)
{
$message = ($pretty_print) ? print_r($message, true) : $message;
// If there is a listener for the log event, we'll delegate the logging
// to the event and not write to the log files. This allows for quick
// swapping of log implementations for debugging.
if (Event::listeners('laravel.log'))
{
Event::fire('laravel.log', array($type, $message));
}
$message = static::format($type, $message);
File::append(path('storage').'logs/'.date('Y-m-d').'.log', $message);
}
/**
* Format a log message for logging.
*
* @param string $type
* @param string $message
* @return string
*/
protected static function format($type, $message)
{
return date('Y-m-d H:i:s').' '.Str::upper($type)." - {$message}".PHP_EOL;
}
/**
* Dynamically write a log message.
*
* <code>
* // Write an "error" message to the log file
* Log::error('This is an error!');
*
* // Write a "warning" message to the log file
* Log::warning('This is a warning!');
*
* // Log an arrays data
* Log::info(array('name' => 'Sawny', 'passwd' => '1234', array(1337, 21, 0)), true);
* //Result: Array ( [name] => Sawny [passwd] => 1234 [0] => Array ( [0] => 1337 [1] => 21 [2] => 0 ) )
* //If we had omit the second parameter the result had been: Array
* </code>
*/
public static function __callStatic($method, $parameters)
{
$parameters[1] = (empty($parameters[1])) ? false : $parameters[1];
static::write($method, $parameters[0], $parameters[1]);
}
}