Completed
Push — develop ( 13c43e...daa4a6 )
by Nate
03:53
created

LoggerTrait::getLogFile()   A

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 create and attach a separate log dispatcher / logger.  It allows modules to log to a separate
16
 * log file, while still supporting the use of categories.
17
 *
18
 * @author Flipbox Factory <[email protected]>
19
 * @since 2.0.0
20
 */
21
trait LoggerTrait
22
{
23
    /**
24
     * The log categories
25
     *
26
     * @param $category
27
     * @return string
28
     */
29
    protected static function loggerCategory($category): string
30
    {
31
        /** @noinspection PhpUndefinedFieldInspection */
32
        $prefix = static::$category ?? '';
33
34
        if (empty($category)) {
35
            return $prefix;
36
        }
37
38
        return ($prefix ? $prefix . ':' : '') . $category;
39
    }
40
41
    /**
42
     * Logs a debug message.
43
     * Trace messages are logged mainly for development purpose to see
44
     * the execution work flow of some code. This method will only log
45
     * a message when the application is in debug mode.
46
     * @param string|array $message the message to be logged. This can be a simple string or a more
47
     * complex data structure, such as array.
48
     * @param string $category the category of the message.
49
     * @since 2.0.14
50
     */
51
    public static function debug($message, $category = 'general')
52
    {
53
        Craft::getLogger()->log($message, Logger::LEVEL_TRACE, static::loggerCategory($category));
54
    }
55
56
    /**
57
     * Logs an error message.
58
     * An error message is typically logged when an unrecoverable error occurs
59
     * during the execution of an application.
60
     * @param string|array $message the message to be logged. This can be a simple string or a more
61
     * complex data structure, such as array.
62
     * @param string $category the category of the message.
63
     */
64
    public static function error($message, $category = 'general')
65
    {
66
        Craft::getLogger()->log($message, Logger::LEVEL_ERROR, static::loggerCategory($category));
67
    }
68
69
    /**
70
     * Logs a warning message.
71
     * A warning message is typically logged when an error occurs while the execution
72
     * can still continue.
73
     * @param string|array $message the message to be logged. This can be a simple string or a more
74
     * complex data structure, such as array.
75
     * @param string $category the category of the message.
76
     */
77
    public static function warning($message, $category = 'general')
78
    {
79
        Craft::getLogger()->log($message, Logger::LEVEL_WARNING, static::loggerCategory($category));
80
    }
81
82
    /**
83
     * Logs an informative message.
84
     * An informative message is typically logged by an application to keep record of
85
     * something important (e.g. an administrator logs in).
86
     * @param string|array $message the message to be logged. This can be a simple string or a more
87
     * complex data structure, such as array.
88
     * @param string $category the category of the message.
89
     */
90
    public static function info($message, $category = 'general')
91
    {
92
        Craft::getLogger()->log($message, Logger::LEVEL_INFO, static::loggerCategory($category));
93
    }
94
}
95