summaryrefslogtreecommitdiff
path: root/lib/Kolab/FreeBusy/Logger.php
blob: abb00600d89090926a123a1ea42e6da04dc312c1 (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
<?php

namespace Kolab\FreeBusy;

/**
 * Helper class for creating up Monolog instanced with local configration
 */
class Logger
{
	private static $instances = array();

	/**
	 * Static getter for a Monolog\Logger instance
	 */
	public static function get($name, $level = 0)
	{
		if (!isset(self::$instances[$name])) {
			$logger = new \Monolog\Logger($name);

			// TODO: support more log drivers
			$config = Config::getInstance();
			switch ($config->get('log.driver')) {
				case 'file':
					$logdir = self::realpath($config->get('log.path'));
					$loglevel = $level ?: $config->get("log.level", \Monolog\Logger::INFO);
					$logger->pushHandler(new \Monolog\Handler\StreamHandler($logdir . $name. '.log', $loglevel));
					break;

				default:
					// null handler if logging is disabled
					$logger->pushHandler(new \Monolog\Handler\NullHandler);
			}

			self::$instances[$name] = $logger;
		}
		
		return self::$instances[$name];
	}

	/**
	 * Resolve the given directory to a real path ending with a /
	 */
	private static function realpath($dirname)
	{
		if ($dirname[0] != '/')
			$dirname = realpath(KOLAB_FREEBUSY_ROOT . '/' . $dirname);

		return rtrim($dirname, '/') . '/';
	}
}