summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Application/Logger/Writer/FileWriter.php
blob: 6b4ed54d95828df54dedfc14c4948d5f7205c0d7 (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
<?php
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */

namespace Icinga\Application\Logger\Writer;

use Exception;
use Icinga\Data\ConfigObject;
use Icinga\Application\Logger;
use Icinga\Application\Logger\LogWriter;
use Icinga\Exception\ConfigurationError;
use Icinga\Util\File;

/**
 * Log to a file
 */
class FileWriter extends LogWriter
{
    /**
     * Path to the file
     *
     * @var string
     */
    protected $file;

    /**
     * Create a new file log writer
     *
     * @param   ConfigObject  $config
     *
     * @throws  ConfigurationError  If the configuration directive 'file' is missing or if the path to 'file' does
     *                              not exist or if writing to 'file' is not possible
     */
    public function __construct(ConfigObject $config)
    {
        if ($config->file === null) {
            throw new ConfigurationError('Required logging configuration directive \'file\' missing');
        }
        $this->file = $config->file;

        if (substr($this->file, 0, 6) !== 'php://' && ! file_exists(dirname($this->file))) {
            throw new ConfigurationError(
                'Log path "%s" does not exist',
                dirname($this->file)
            );
        }

        try {
            $this->write(''); // Avoid to handle such errors on every write access
        } catch (Exception $e) {
            throw new ConfigurationError(
                'Cannot write to log file "%s" (%s)',
                $this->file,
                $e->getMessage()
            );
        }
    }

    /**
     * Log a message
     *
     * @param   int     $level      The logging level
     * @param   string  $message    The log message
     */
    public function log($level, $message)
    {
        $this->write(date('c') . ' - ' . Logger::$levels[$level] . ' - ' . $message . PHP_EOL);
    }

    /**
     * Write a message to the log
     *
     * @param string $message
     */
    protected function write($message)
    {
        $file = new File($this->file, 'a');
        $file->fwrite($message);
        $file->fflush();
    }
}