|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
/** |
|
4
|
|
|
* Created by PhpStorm. |
|
5
|
|
|
* User: benedikt |
|
6
|
|
|
* Date: 1/5/18 |
|
7
|
|
|
* Time: 11:16 PM |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Tfboe\FmLib\Helpers; |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
use Monolog\Handler\StreamHandler; |
|
14
|
|
|
use Monolog\Logger; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class Logging |
|
18
|
|
|
* @package Tfboe\FmLib\Helpers |
|
19
|
|
|
*/ |
|
20
|
|
|
abstract class Logging |
|
21
|
|
|
{ |
|
22
|
|
|
//<editor-fold desc="Fields"> |
|
23
|
|
|
/** @var bool */ |
|
24
|
|
|
public static $testing = false; |
|
25
|
|
|
public static $storagePathFunction = 'storage_path'; |
|
26
|
|
|
/** @var Logger[] */ |
|
27
|
|
|
private static $loggers = []; |
|
28
|
|
|
//</editor-fold desc="Fields"> |
|
29
|
|
|
|
|
30
|
|
|
//<editor-fold desc="Public Methods"> |
|
31
|
|
|
/** @noinspection PhpDocMissingThrowsInspection */ //InvalidArgumentException, Exception |
|
32
|
|
|
/** |
|
33
|
|
|
* Logs the given message in the given logger |
|
34
|
|
|
* @param string $message the message to log |
|
35
|
|
|
* @param string $logger the logger to use |
|
36
|
|
|
* @param int $type the type of the message |
|
37
|
|
|
* @throws \Tfboe\FmLib\Exceptions\ValueNotValid invalid logger |
|
38
|
|
|
*/ |
|
39
|
|
|
public static function log(string $message, string $logger = Logs::GENERAL, int $type = Logger::INFO) |
|
40
|
|
|
{ |
|
41
|
|
|
if (self::$testing && $logger !== Logs::TESTING) { |
|
42
|
|
|
//do nothing |
|
43
|
|
|
return; |
|
44
|
|
|
} |
|
45
|
|
|
Logs::ensureValidValue($logger); |
|
46
|
|
|
if (!array_key_exists($logger, self::$loggers)) { |
|
47
|
|
|
self::$loggers[$logger] = new Logger($logger); |
|
48
|
|
|
// InvalidArgumentException => stream is a string |
|
49
|
|
|
// Exception => path is static and it is ensured that it is valid |
|
50
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
|
51
|
|
|
self::$loggers[$logger]->pushHandler( |
|
52
|
|
|
new StreamHandler((self::$storagePathFunction)() . '/logs/' . $logger . '.log')); |
|
53
|
|
|
} |
|
54
|
|
|
self::$loggers[$logger]->log($type, $message); |
|
55
|
|
|
} |
|
56
|
|
|
//</editor-fold desc="Public Methods"> |
|
57
|
|
|
} |