LoggerTrait::getLogger()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-ember/
7
 */
8
9
namespace flipbox\craft\ember\modules;
10
11
use Craft;
12
use yii\log\Logger;
13
14
/**
15
 * This trait will prefix log items to a specific category.
16
 *
17
 * To properly implement, add a static $category attribute to your module such as:
18
 *
19
 * ```public static $category = 'some-category';```
20
 *
21
 * Then log a message through the module using the static log methods found within this trait.
22
 *
23
 * Additionally, you can log to a separate log file by appending log targets via the `config/app.php` file
24
 *
25
 * ```php
26
 * [
27
 *  'components' => [
28
 *      'log' => function() {
29
 *          $config = craft\helpers\App::logConfig();
30
 *
31
 *          $targetConfigs = \flipbox\craft\ember\helpers\LoggerHelper::targetConfigs(
32
 *              ['some-category', 'some-other-category']
33
 *          );
34
 *
35
 *          foreach ($targetConfigs as $key => $targetConfig) {
36
 *              $config['targets'][$key] = $targetConfig;
37
 *          }
38
 *
39
 *          return $config ? Craft::createObject($config) : null;
40
 *      }
41
 *  ]
42
 * ]
43
 * ```
44
 *
45
 * @author Flipbox Factory <[email protected]>
46
 * @since 2.0.0
47
 */
48
trait LoggerTrait
49
{
50
    /**
51
     * @return Logger
52
     *
53
     * @deprecated
54
     */
55
    public static function getLogger()
56
    {
57
        return Craft::getLogger();
58
    }
59
60
    /**
61
     * The log categories
62
     *
63
     * @param string|null $category
64
     * @param bool $audit flag as an audit message.
65
     * @return string
66
     */
67
    protected static function loggerCategory(string $category = null, bool $audit = false): string
68
    {
69
        /** @noinspection PhpUndefinedFieldInspection */
70
        $prefix = static::$category ? (static::$category . ($audit ? ':audit' : '')) : '';
71
72
        if (empty($category)) {
73
            return $prefix;
74
        }
75
76
        return ($prefix ? $prefix . ':' : '') . $category;
77
    }
78
79
    /**
80
     * Logs a debug message.
81
     * Trace messages are logged mainly for development purpose to see
82
     * the execution work flow of some code. This method will only log
83
     * a message when the application is in debug mode.
84
     * @param string|array $message the message to be logged. This can be a simple string or a more
85
     * complex data structure, such as array.
86
     * @param string $category the category of the message.
87
     * @param bool $audit flag as an audit message.
88
     * @since 2.0.0
89
     */
90
    public static function debug($message, $category = 'general', bool $audit = false)
91
    {
92
        Craft::getLogger()->log($message, Logger::LEVEL_TRACE, static::loggerCategory($category, $audit));
93
    }
94
95
    /**
96
     * Logs an error message.
97
     * An error message is typically logged when an unrecoverable error occurs
98
     * during the execution of an application.
99
     * @param string|array $message the message to be logged. This can be a simple string or a more
100
     * complex data structure, such as array.
101
     * @param string $category the category of the message.
102
     * @param bool $audit flag as an audit message.
103
     * @since 2.0.0
104
     */
105
    public static function error($message, $category = 'general', bool $audit = false)
106
    {
107
        Craft::getLogger()->log($message, Logger::LEVEL_ERROR, static::loggerCategory($category, $audit));
108
    }
109
110
    /**
111
     * Logs a warning message.
112
     * A warning message is typically logged when an error occurs while the execution
113
     * can still continue.
114
     * @param string|array $message the message to be logged. This can be a simple string or a more
115
     * complex data structure, such as array.
116
     * @param string $category the category of the message.
117
     * @param bool $audit flag as an audit message.
118
     * @since 2.0.0
119
     */
120
    public static function warning($message, $category = 'general', bool $audit = false)
121
    {
122
        Craft::getLogger()->log($message, Logger::LEVEL_WARNING, static::loggerCategory($category, $audit));
123
    }
124
125
    /**
126
     * Logs an informative message.
127
     * An informative message is typically logged by an application to keep record of
128
     * something important (e.g. an administrator logs in).
129
     * @param string|array $message the message to be logged. This can be a simple string or a more
130
     * complex data structure, such as array.
131
     * @param string $category the category of the message.
132
     * @param bool $audit flag as an audit message.
133
     * @since 2.0.0
134
     */
135
    public static function info($message, $category = 'general', bool $audit = false)
136
    {
137
        Craft::getLogger()->log($message, Logger::LEVEL_INFO, static::loggerCategory($category, $audit));
138
    }
139
}
140