Completed
Push — master ( 1f833c...453612 )
by Mikael
02:43
created

FileLogger   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 10
dl 0
loc 25
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A log() 0 14 2
1
<?php
2
3
namespace Anax\Log;
4
5
use Psr\Log\AbstractLogger;
6
use Psr\Log\LoggerInterface;
7
8
/**
9
 * Anax Logger class.
10
 *
11
 * The message MUST be a string or object implementing __toString().
12
 *
13
 * The message MAY contain placeholders in the form: {foo} where foo
14
 * will be replaced by the context data in key "foo".
15
 *
16
 * The context array can contain arbitrary data, the only assumption that
17
 * can be made by implementors is that if an Exception instance is given
18
 * to produce a stack trace, it MUST be in a key named "exception".
19
 *
20
 * See https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md
21
 * for the full interface specification.
22
 */
23
class FileLogger extends AbstractLogger
24
{
25
    /**
26
     * Logs with an arbitrary level.
27
     *
28
     * @param mixed  $level   the loglevel to use for this entry.
29
     * @param string $message to log.
30
     * @param array  $context extra information to log.
31
     *
32
     * @return null
33
     */
34
    public function log($level, $message, array $context = [])
35
    {
36
        $file = ANAX_INSTALL_PATH . "/log/dev_log";
37
        $dateTime = new \DateTime("now", new \DateTimeZone("Europe/Stockholm"));
38
        $date = $dateTime->format("Y-m-d H:m:s e");
39
40
        $contextString = "";
41
        foreach ($context as $key => $val) {
42
            $contextString .= " $key=" . htmlentities($val) . ",";
43
        }
44
        $contextString = rtrim($contextString, ",");
45
46
        $record = "[$date] [$level] {$message}{$contextString}\n";
47
        file_put_contents($file, $record, FILE_APPEND);
48
    }
49
}
50