1: <?php namespace Laravel;
2:
3: class Log {
4:
5: /**
6: * Log an exception to the log file.
7: *
8: * @param Exception $e
9: * @return void
10: */
11: public static function exception($e)
12: {
13: static::write('error', static::exception_line($e));
14: }
15:
16: /**
17: * Format a log friendly message from the given exception.
18: *
19: * @param Exception $e
20: * @return string
21: */
22: protected static function exception_line($e)
23: {
24: return $e->getMessage().' in '.$e->getFile().' on line '.$e->getLine();
25: }
26:
27: /**
28: * Write a message to the log file.
29: *
30: * <code>
31: * // Write an "error" message to the log file
32: * Log::write('error', 'Something went horribly wrong!');
33: *
34: * // Write an "error" message using the class' magic method
35: * Log::error('Something went horribly wrong!');
36: *
37: * // Log an arrays data
38: * Log::write('info', array('name' => 'Sawny', 'passwd' => '1234', array(1337, 21, 0)), true);
39: * //Result: Array ( [name] => Sawny [passwd] => 1234 [0] => Array ( [0] => 1337 [1] => 21 [2] => 0 ) )
40: * //If we had omit the third parameter the result had been: Array
41: * </code>
42: *
43: * @param string $type
44: * @param string $message
45: * @return void
46: */
47: public static function write($type, $message, $pretty_print = false)
48: {
49: $message = ($pretty_print) ? print_r($message, true) : $message;
50:
51: // If there is a listener for the log event, we'll delegate the logging
52: // to the event and not write to the log files. This allows for quick
53: // swapping of log implementations for debugging.
54: if (Event::listeners('laravel.log'))
55: {
56: Event::fire('laravel.log', array($type, $message));
57: }
58:
59: $trace=debug_backtrace();
60:
61: foreach($trace as $item)
62: {
63: if (isset($item['class']) AND $item['class'] == __CLASS__)
64: {
65: continue;
66: }
67:
68: $caller = $item;
69:
70: break;
71: }
72:
73: $function = $caller['function'];
74: if (isset($caller['class']))
75: {
76: $class = $caller['class'] . '::';
77: }
78: else
79: {
80: $class = '';
81: }
82:
83: $message = static::format($type, $class . $function . ' - ' . $message);
84:
85: File::append(path('storage').'logs/'.date('Y-m-d').'.log', $message);
86: }
87:
88: /**
89: * Format a log message for logging.
90: *
91: * @param string $type
92: * @param string $message
93: * @return string
94: */
95: protected static function format($type, $message)
96: {
97: return date('Y-m-d H:i:s').' '.Str::upper($type)." - {$message}".PHP_EOL;
98: }
99:
100: /**
101: * Dynamically write a log message.
102: *
103: * <code>
104: * // Write an "error" message to the log file
105: * Log::error('This is an error!');
106: *
107: * // Write a "warning" message to the log file
108: * Log::warning('This is a warning!');
109: *
110: * // Log an arrays data
111: * Log::info(array('name' => 'Sawny', 'passwd' => '1234', array(1337, 21, 0)), true);
112: * //Result: Array ( [name] => Sawny [passwd] => 1234 [0] => Array ( [0] => 1337 [1] => 21 [2] => 0 ) )
113: * //If we had omit the second parameter the result had been: Array
114: * </code>
115: */
116: public static function __callStatic($method, $parameters)
117: {
118: $parameters[1] = (empty($parameters[1])) ? false : $parameters[1];
119:
120: static::write($method, $parameters[0], $parameters[1]);
121: }
122:
123: }
124: