1: <?php
2: /**
3: * ManiaLive - TrackMania dedicated server manager in PHP
4: *
5: * @copyright Copyright (c) 2009-2011 NADEO (http://www.nadeo.com)
6: * @license http://www.gnu.org/licenses/lgpl.html LGPL License 3
7: * @version $Revision: 446 $:
8: * @author $Author: martin.gwendal $:
9: * @date $Date: 2012-08-28 15:00:56 +0200 (mar., 28 août 2012) $:
10: */
11:
12: namespace ManiaLive\Utilities;
13:
14: class Logger
15: {
16: private static $logs = array();
17: private $enabled;
18: private $path;
19:
20: /**
21: * @param string $name
22: * @return \ManiaLive\Utilities\Logger
23: */
24: static function getLog($name)
25: {
26: if(isset(self::$logs[$name]))
27: return self::$logs[$name];
28:
29: $log = new Logger($name);
30: self::$logs[$name] = $log;
31: return $log;
32: }
33:
34: private function __construct($name)
35: {
36: // if path does not exist ...
37: $config = \ManiaLive\Config\Config::getInstance();
38: if(!is_dir($config->logsPath))
39: mkdir($config->logsPath, '0777', true);
40:
41: $this->path = $config->logsPath.'/'.($config->logsPrefix ? $config->logsPrefix.'-' : '').$name.'.txt';
42: $this->enabled = true;
43: }
44:
45: function enableLog()
46: {
47: $this->enabled = true;
48: }
49:
50: function disableLog()
51: {
52: $this->enabled = false;
53: }
54:
55: function write($text, $tags=array())
56: {
57: if($this->enabled)
58: {
59: array_unshift($tags, date('Y.m.d H:i:s'));
60: file_put_contents($this->path, '['.implode('][', $tags).'] '.$text.PHP_EOL, FILE_APPEND);
61: }
62: }
63: }
64:
65: ?>