PluginProbe
Loginizer / trunk
Loginizer vtrunk
2.1.0 2.0.9 2.0.8 1.9.8 1.9.9 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 trunk 1.0 1.0.1 1.0.2 1.1.0 1.1.1 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 All 74 releases
loginizer / lib / hybridauth / Logger / Logger.php

Logger.php in Loginizer trunk, at lib/hybridauth/Logger/Logger.php

130 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*!
3 * Hybridauth
4 * https://hybridauth.github.io | https://github.com/hybridauth/hybridauth
5 * (c) 2017 Hybridauth authors | https://hybridauth.github.io/license.html
6 */
7
8 namespace Hybridauth\Logger;
9
10 use Hybridauth\Exception\RuntimeException;
11 use Hybridauth\Exception\InvalidArgumentException;
12
13 /**
14 * Debugging and Logging utility.
15 */
16 class Logger implements LoggerInterface
17 {
18 const NONE = 'none'; // turn logging off
19 const DEBUG = 'debug'; // debug, info and error messages
20 const INFO = 'info'; // info and error messages
21 const ERROR = 'error'; // only error messages
22
23 /**
24 * Debug level.
25 *
26 * One of Logger::NONE, Logger::DEBUG, Logger::INFO, Logger::ERROR
27 *
28 * @var string
29 */
30 protected $level;
31
32 /**
33 * Path to file writeable by the web server. Required if $this->level !== Logger::NONE.
34 *
35 * @var string
36 */
37 protected $file;
38
39 /**
40 * @param bool|string $level One of Logger::NONE, Logger::DEBUG, Logger::INFO, Logger::ERROR
41 * @param string $file File where to write messages
42 *
43 * @throws InvalidArgumentException
44 * @throws RuntimeException
45 */
46 public function __construct($level, $file)
47 {
48 $this->level = self::NONE;
49
50 if ($level && $level !== self::NONE) {
51 $this->initialize($file);
52
53 $this->level = $level === true ? Logger::DEBUG : $level;
54 $this->file = $file;
55 }
56 }
57
58 /**
59 * @param string $file
60 *
61 * @throws InvalidArgumentException
62 * @throws RuntimeException
63 */
64 protected function initialize($file)
65 {
66 if (!$file) {
67 throw new InvalidArgumentException('Log file is not specified.');
68 }
69
70 if (!file_exists($file) && !touch($file)) {
71 throw new RuntimeException(sprintf('Log file %s can not be created.', $file));
72 }
73
74 if (!is_writable($file)) {
75 throw new RuntimeException(sprintf('Log file %s is not writeable.', $file));
76 }
77 }
78
79 /**
80 * @inheritdoc
81 */
82 public function info($message, array $context = [])
83 {
84 if (!in_array($this->level, [self::DEBUG, self::INFO])) {
85 return;
86 }
87
88 $this->log(self::INFO, $message, $context);
89 }
90
91 /**
92 * @inheritdoc
93 */
94 public function debug($message, array $context = [])
95 {
96 if (!in_array($this->level, [self::DEBUG])) {
97 return;
98 }
99
100 $this->log(self::DEBUG, $message, $context);
101 }
102
103 /**
104 * @inheritdoc
105 */
106 public function error($message, array $context = [])
107 {
108 if (!in_array($this->level, [self::DEBUG, self::INFO, self::ERROR])) {
109 return;
110 }
111
112 $this->log(self::ERROR, $message, $context);
113 }
114
115 /**
116 * @inheritdoc
117 */
118 public function log($level, $message, array $context = [])
119 {
120 $datetime = new \DateTime();
121 $datetime = $datetime->format(DATE_ATOM);
122
123 $content = sprintf('%s -- %s -- %s -- %s', $level, $_SERVER['REMOTE_ADDR'], $datetime, $message);
124 $content .= ($context ? "\n" . print_r($context, true) : '');
125 $content .= "\n";
126
127 file_put_contents($this->file, $content, FILE_APPEND);
128 }
129 }
130