summaryrefslogtreecommitdiff
path: root/system/libraries/Kohana_Log.php
blob: 44ef8af8a5b9868097df66ef4533ecb62eaead61 (plain)
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
<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
 * Logging class.
 *
 * $Id: Kohana_Log.php 4679 2009-11-10 01:45:52Z isaiah $
 *
 * @package    Core
 * @author     Kohana Team
 * @copyright  (c) 2007-2009 Kohana Team
 * @license    http://kohanaphp.com/license
 */
class Kohana_Log_Core {

	// Configuration
	protected static $config;

	// Drivers
	protected static $drivers;

	// Logged messages
	protected static $messages;

	/**
	 * Add a new message to the log.
	 *
	 * @param   string  type of message
	 * @param   string  message text
	 * @return  void
	 */
	public static function add($type, $message)
	{
		// Make sure the drivers and config are loaded
		if ( ! is_array(Kohana_Log::$config))
		{
			Kohana_Log::$config = Kohana::config('log');
		}

		if ( ! is_array(Kohana_Log::$drivers))
		{
			foreach ( (array) Kohana::config('log.drivers') as $driver_name)
			{
				// Set driver name
				$driver = 'Log_'.ucfirst($driver_name).'_Driver';

				// Load the driver
				if ( ! Kohana::auto_load($driver))
					throw new Kohana_Exception('Log Driver Not Found: %driver%', array('%driver%' => $driver));

				// Initialize the driver
				$driver = new $driver(array_merge(Kohana::config('log'), Kohana::config('log_'.$driver_name)));

				// Validate the driver
				if ( ! ($driver instanceof Log_Driver))
					throw new Kohana_Exception('%driver% does not implement the Log_Driver interface', array('%driver%' => $driver));

				Kohana_Log::$drivers[] = $driver;
			}

			// Always save logs on shutdown
			Event::add('system.shutdown', array('Kohana_Log', 'save'));
		}

		Kohana_Log::$messages[] = array('date' => time(), 'type' => $type, 'message' => $message);
	}

	/**
	 * Save all currently logged messages.
	 *
	 * @return  void
	 */
	public static function save()
	{
		if (empty(Kohana_Log::$messages))
			return;

		foreach (Kohana_Log::$drivers as $driver)
		{
			// We can't throw exceptions here or else we will get a
			// Exception thrown without a stack frame error
			try
			{
				$driver->save(Kohana_Log::$messages);
			}
			catch(Exception $e){}
		}

		// Reset the messages
		Kohana_Log::$messages = array();
	}
}