@athenna/logger 3.0.9 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/Drivers/ConsoleDriver.d.ts +12 -0
- package/build/Drivers/ConsoleDriver.js +19 -0
- package/build/Drivers/DiscordDriver.d.ts +12 -0
- package/build/Drivers/DiscordDriver.js +22 -0
- package/build/Drivers/Driver.d.ts +52 -0
- package/build/Drivers/Driver.js +85 -0
- package/build/Drivers/FileDriver.d.ts +12 -0
- package/build/Drivers/FileDriver.js +24 -0
- package/build/Drivers/NullDriver.d.ts +12 -0
- package/build/Drivers/NullDriver.js +14 -0
- package/build/Drivers/SlackDriver.d.ts +12 -0
- package/build/Drivers/SlackDriver.js +19 -0
- package/build/Drivers/StackDriver.d.ts +12 -0
- package/build/Drivers/StackDriver.js +19 -0
- package/build/Drivers/TelegramDriver.d.ts +12 -0
- package/build/Drivers/TelegramDriver.js +21 -0
- package/build/Exceptions/DriverExistException.d.ts +12 -0
- package/build/Exceptions/DriverExistException.js +21 -0
- package/{src/Facades/Log.js → build/Exceptions/FormatterExistException.d.ts} +4 -9
- package/build/Exceptions/FormatterExistException.js +21 -0
- package/build/Exceptions/NotFoundDriverException.d.ts +12 -0
- package/build/Exceptions/NotFoundDriverException.js +21 -0
- package/build/Exceptions/NotFoundFormatterException.d.ts +12 -0
- package/build/Exceptions/NotFoundFormatterException.js +21 -0
- package/build/Exceptions/NotImplementedConfigException.d.ts +12 -0
- package/build/Exceptions/NotImplementedConfigException.js +28 -0
- package/build/Facades/Log.d.ts +10 -0
- package/build/Facades/Log.js +10 -0
- package/build/Factories/DriverFactory.d.ts +39 -0
- package/build/Factories/DriverFactory.js +93 -0
- package/build/Factories/FormatterFactory.d.ts +29 -0
- package/build/Factories/FormatterFactory.js +57 -0
- package/build/Formatters/CliFormatter.d.ts +12 -0
- package/build/Formatters/CliFormatter.js +15 -0
- package/build/Formatters/Formatter.d.ts +85 -0
- package/build/Formatters/Formatter.js +177 -0
- package/build/Formatters/JsonFormatter.d.ts +12 -0
- package/build/Formatters/JsonFormatter.js +26 -0
- package/build/Formatters/MessageFormatter.d.ts +12 -0
- package/build/Formatters/MessageFormatter.js +14 -0
- package/build/Formatters/NoneFormatter.d.ts +12 -0
- package/{src → build}/Formatters/NoneFormatter.js +4 -12
- package/build/Formatters/RequestFormatter.d.ts +12 -0
- package/build/Formatters/RequestFormatter.js +45 -0
- package/build/Formatters/SimpleFormatter.d.ts +12 -0
- package/build/Formatters/SimpleFormatter.js +18 -0
- package/build/Helpers/ColorHelper.d.ts +131 -0
- package/build/Helpers/ColorHelper.js +215 -0
- package/build/Helpers/FactoryHelper.d.ts +14 -0
- package/build/Helpers/FactoryHelper.js +20 -0
- package/build/Logger/Logger.d.ts +68 -0
- package/build/Logger/Logger.js +105 -0
- package/build/Logger/VanillaLogger.d.ts +60 -0
- package/build/Logger/VanillaLogger.js +86 -0
- package/build/Providers/LoggerProvider.d.ts +12 -0
- package/{src → build}/Providers/LoggerProvider.js +5 -12
- package/build/index.d.ts +18 -0
- package/build/index.js +18 -0
- package/package.json +81 -59
- package/src/Drivers/ConsoleDriver.js +0 -40
- package/src/Drivers/DiscordDriver.js +0 -43
- package/src/Drivers/Driver.js +0 -134
- package/src/Drivers/FileDriver.js +0 -47
- package/src/Drivers/NullDriver.js +0 -33
- package/src/Drivers/SlackDriver.js +0 -40
- package/src/Drivers/StackDriver.js +0 -45
- package/src/Drivers/TelegramDriver.js +0 -46
- package/src/Exceptions/DriverExistException.js +0 -31
- package/src/Exceptions/FormatterExistException.js +0 -32
- package/src/Exceptions/NotFoundDriverException.js +0 -31
- package/src/Exceptions/NotFoundFormatterException.js +0 -31
- package/src/Exceptions/NotImplementedConfigException.js +0 -37
- package/src/Factories/DriverFactory.js +0 -128
- package/src/Factories/FormatterFactory.js +0 -77
- package/src/Formatters/CliFormatter.js +0 -24
- package/src/Formatters/Formatter.js +0 -266
- package/src/Formatters/JsonFormatter.js +0 -37
- package/src/Formatters/MessageFormatter.js +0 -26
- package/src/Formatters/RequestFormatter.js +0 -64
- package/src/Formatters/SimpleFormatter.js +0 -27
- package/src/Helpers/ColorHelper.js +0 -322
- package/src/Helpers/FactoryHelper.js +0 -31
- package/src/Helpers/VanillaLogger.js +0 -134
- package/src/index.d.ts +0 -797
- package/src/index.js +0 -180
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @athenna/logger
|
|
3
|
-
*
|
|
4
|
-
* (c) João Lenon <lenon@athenna.io>
|
|
5
|
-
*
|
|
6
|
-
* For the full copyright and license information, please view the LICENSE
|
|
7
|
-
* file that was distributed with this source code.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import { Exception } from '@athenna/common'
|
|
11
|
-
import { DriverFactory } from '#src/Factories/DriverFactory'
|
|
12
|
-
|
|
13
|
-
export class DriverExistException extends Exception {
|
|
14
|
-
/**
|
|
15
|
-
* Creates a new instance of DriverExistException.
|
|
16
|
-
*
|
|
17
|
-
* @param {string} driverName
|
|
18
|
-
* @return {DriverExistException}
|
|
19
|
-
*/
|
|
20
|
-
constructor(driverName) {
|
|
21
|
-
const content = `The driver ${driverName} already exists in DriverFactory.`
|
|
22
|
-
const availableDrivers = DriverFactory.availableDrivers().join(', ')
|
|
23
|
-
|
|
24
|
-
super(
|
|
25
|
-
content,
|
|
26
|
-
500,
|
|
27
|
-
'E_EXIST_DRIVER',
|
|
28
|
-
`Available drivers are: ${availableDrivers}. The name ${driverName} is already in use inside DriverFactory. Try using a different name for your driver implementation.`,
|
|
29
|
-
)
|
|
30
|
-
}
|
|
31
|
-
}
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @athenna/logger
|
|
3
|
-
*
|
|
4
|
-
* (c) João Lenon <lenon@athenna.io>
|
|
5
|
-
*
|
|
6
|
-
* For the full copyright and license information, please view the LICENSE
|
|
7
|
-
* file that was distributed with this source code.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import { Exception } from '@athenna/common'
|
|
11
|
-
import { FormatterFactory } from '#src/Factories/FormatterFactory'
|
|
12
|
-
|
|
13
|
-
export class FormatterExistException extends Exception {
|
|
14
|
-
/**
|
|
15
|
-
* Creates a new instance of FormatterExistException.
|
|
16
|
-
*
|
|
17
|
-
* @param {string} formatterName
|
|
18
|
-
* @return {FormatterExistException}
|
|
19
|
-
*/
|
|
20
|
-
constructor(formatterName) {
|
|
21
|
-
const content = `The formatter ${formatterName} already exists in FormatterFactory.`
|
|
22
|
-
const availableFormatters =
|
|
23
|
-
FormatterFactory.availableFormatters().join(', ')
|
|
24
|
-
|
|
25
|
-
super(
|
|
26
|
-
content,
|
|
27
|
-
500,
|
|
28
|
-
'E_EXIST_FORMATTER',
|
|
29
|
-
`Available formatters are: ${availableFormatters}. The name ${formatterName} is already in use inside FormatterFactory. Try using a different name for your formatter implementation.`,
|
|
30
|
-
)
|
|
31
|
-
}
|
|
32
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @athenna/logger
|
|
3
|
-
*
|
|
4
|
-
* (c) João Lenon <lenon@athenna.io>
|
|
5
|
-
*
|
|
6
|
-
* For the full copyright and license information, please view the LICENSE
|
|
7
|
-
* file that was distributed with this source code.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import { Exception, Path } from '@athenna/common'
|
|
11
|
-
import { DriverFactory } from '#src/Factories/DriverFactory'
|
|
12
|
-
|
|
13
|
-
export class NotFoundDriverException extends Exception {
|
|
14
|
-
/**
|
|
15
|
-
* Creates a new instance of NotFoundDriverException.
|
|
16
|
-
*
|
|
17
|
-
* @param {string} driverName
|
|
18
|
-
* @return {NotFoundDriverException}
|
|
19
|
-
*/
|
|
20
|
-
constructor(driverName) {
|
|
21
|
-
const content = `The driver ${driverName} has not been found.`
|
|
22
|
-
const availableDrivers = DriverFactory.availableDrivers().join(', ')
|
|
23
|
-
|
|
24
|
-
super(
|
|
25
|
-
content,
|
|
26
|
-
500,
|
|
27
|
-
'E_NOT_FOUND',
|
|
28
|
-
`Available drivers are: ${availableDrivers}. Look into your config/logger.${Path.ext()} file if ${driverName} driver is implemented by logger. Or create ${driverName} driver implementation using DriverFactory.createDriver("${driverName}", ...) method.`,
|
|
29
|
-
)
|
|
30
|
-
}
|
|
31
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @athenna/logger
|
|
3
|
-
*
|
|
4
|
-
* (c) João Lenon <lenon@athenna.io>
|
|
5
|
-
*
|
|
6
|
-
* For the full copyright and license information, please view the LICENSE
|
|
7
|
-
* file that was distributed with this source code.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import { Exception } from '@athenna/common'
|
|
11
|
-
import { FormatterFactory } from '#src/Factories/FormatterFactory'
|
|
12
|
-
|
|
13
|
-
export class NotFoundFormatterException extends Exception {
|
|
14
|
-
/**
|
|
15
|
-
* Creates a new instance of NotFoundFormatterException.
|
|
16
|
-
*
|
|
17
|
-
* @param {string} formatterName
|
|
18
|
-
* @return {NotFoundFormatterException}
|
|
19
|
-
*/
|
|
20
|
-
constructor(formatterName) {
|
|
21
|
-
const content = `The formatter ${formatterName} has not been found.`
|
|
22
|
-
const availableDrivers = FormatterFactory.availableFormatters().join(', ')
|
|
23
|
-
|
|
24
|
-
super(
|
|
25
|
-
content,
|
|
26
|
-
500,
|
|
27
|
-
'E_NOT_FOUND',
|
|
28
|
-
`Available formatters are: ${availableDrivers}. Look into your config/logger.js file if ${formatterName} formatter is implemented by logger. Or create ${formatterName} formatter implementation using FormatterFactory.createFormatter("${formatterName}", ...) method.`,
|
|
29
|
-
)
|
|
30
|
-
}
|
|
31
|
-
}
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @athenna/logger
|
|
3
|
-
*
|
|
4
|
-
* (c) João Lenon <lenon@athenna.io>
|
|
5
|
-
*
|
|
6
|
-
* For the full copyright and license information, please view the LICENSE
|
|
7
|
-
* file that was distributed with this source code.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import { Path, Exception } from '@athenna/common'
|
|
11
|
-
|
|
12
|
-
export class NotImplementedConfigException extends Exception {
|
|
13
|
-
/**
|
|
14
|
-
* Creates a new instance of NotImplementedConfigException.
|
|
15
|
-
*
|
|
16
|
-
* @param {string} channelName
|
|
17
|
-
* @param {any[]} channels
|
|
18
|
-
* @return {NotImplementedConfigException}
|
|
19
|
-
*/
|
|
20
|
-
constructor(channelName, channels) {
|
|
21
|
-
const content = `Channel ${channelName} is not configured inside logging.channels object from config/logging.${Path.ext()} file.`
|
|
22
|
-
|
|
23
|
-
let help = ''
|
|
24
|
-
|
|
25
|
-
if (channels && channels.length) {
|
|
26
|
-
const availableConfigs = Object.keys(channels).join(', ')
|
|
27
|
-
|
|
28
|
-
help += `Available configurations are: ${availableConfigs}.`
|
|
29
|
-
} else {
|
|
30
|
-
help += `The "Config.get('logging.channels')" is empty, maybe your configuration files are not loaded?`
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
help += ` Create your configuration inside channels object to use it. Or load your configuration files using "Config.safeLoad(Path.config('logging.${Path.ext()}'))`
|
|
34
|
-
|
|
35
|
-
super(content, 500, 'E_NOT_IMPLEMENTED_CONFIG_ERROR', help)
|
|
36
|
-
}
|
|
37
|
-
}
|
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @athenna/logger
|
|
3
|
-
*
|
|
4
|
-
* (c) João Lenon <lenon@athenna.io>
|
|
5
|
-
*
|
|
6
|
-
* For the full copyright and license information, please view the LICENSE
|
|
7
|
-
* file that was distributed with this source code.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import { Config } from '@athenna/config'
|
|
11
|
-
|
|
12
|
-
import { FactoryHelper } from '#src/index'
|
|
13
|
-
import { FileDriver } from '#src/Drivers/FileDriver'
|
|
14
|
-
import { NullDriver } from '#src/Drivers/NullDriver'
|
|
15
|
-
import { SlackDriver } from '#src/Drivers/SlackDriver'
|
|
16
|
-
import { StackDriver } from '#src/Drivers/StackDriver'
|
|
17
|
-
import { ConsoleDriver } from '#src/Drivers/ConsoleDriver'
|
|
18
|
-
import { DiscordDriver } from '#src/Drivers/DiscordDriver'
|
|
19
|
-
import { TelegramDriver } from '#src/Drivers/TelegramDriver'
|
|
20
|
-
import { DriverExistException } from '#src/Exceptions/DriverExistException'
|
|
21
|
-
import { NotFoundDriverException } from '#src/Exceptions/NotFoundDriverException'
|
|
22
|
-
import { NotImplementedConfigException } from '#src/Exceptions/NotImplementedConfigException'
|
|
23
|
-
import { Options } from '@athenna/common'
|
|
24
|
-
|
|
25
|
-
export class DriverFactory {
|
|
26
|
-
/**
|
|
27
|
-
* Drivers of DriverFactory.
|
|
28
|
-
*
|
|
29
|
-
* @type {Map<string, { Driver: any }>}
|
|
30
|
-
*/
|
|
31
|
-
static #drivers = new Map()
|
|
32
|
-
.set('file', { Driver: FileDriver })
|
|
33
|
-
.set('null', { Driver: NullDriver })
|
|
34
|
-
.set('slack', { Driver: SlackDriver })
|
|
35
|
-
.set('stack', { Driver: StackDriver })
|
|
36
|
-
.set('console', { Driver: ConsoleDriver })
|
|
37
|
-
.set('discord', { Driver: DiscordDriver })
|
|
38
|
-
.set('telegram', { Driver: TelegramDriver })
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Return an array with all available drivers.
|
|
42
|
-
*
|
|
43
|
-
* @return {any[]}
|
|
44
|
-
*/
|
|
45
|
-
static availableDrivers() {
|
|
46
|
-
const availableDrivers = []
|
|
47
|
-
|
|
48
|
-
for (const key of this.#drivers.keys()) {
|
|
49
|
-
availableDrivers.push(key)
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
return availableDrivers
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Fabricate a new instance of a driver based on
|
|
57
|
-
* channel configurations.
|
|
58
|
-
*
|
|
59
|
-
* @param {string} channelName
|
|
60
|
-
* @param {any} runtimeConfig
|
|
61
|
-
* @return {any}
|
|
62
|
-
*/
|
|
63
|
-
static fabricate(channelName, runtimeConfig = {}) {
|
|
64
|
-
const channelConfig = this.#getChannelConfig(channelName)
|
|
65
|
-
|
|
66
|
-
const { Driver } = this.#drivers.get(channelConfig.driver)
|
|
67
|
-
|
|
68
|
-
const configs = FactoryHelper.groupConfigs(runtimeConfig, channelConfig)
|
|
69
|
-
|
|
70
|
-
return new Driver(configs)
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Fabricate a new instance of a driver with vanilla
|
|
75
|
-
* configurations.
|
|
76
|
-
*
|
|
77
|
-
* @param {any} configs
|
|
78
|
-
* @return {any}
|
|
79
|
-
*/
|
|
80
|
-
static fabricateVanilla(configs = {}) {
|
|
81
|
-
configs = Options.create(configs, {
|
|
82
|
-
driver: 'console',
|
|
83
|
-
formatter: 'none',
|
|
84
|
-
})
|
|
85
|
-
|
|
86
|
-
const { Driver } = this.#drivers.get(configs.driver)
|
|
87
|
-
|
|
88
|
-
return new Driver(configs)
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Creates a new driver implementation.
|
|
93
|
-
*
|
|
94
|
-
* @param {string} name
|
|
95
|
-
* @param {(channel: string, configs?: any) => any} driver
|
|
96
|
-
*/
|
|
97
|
-
static createDriver(name, driver) {
|
|
98
|
-
if (this.#drivers.has(name)) {
|
|
99
|
-
throw new DriverExistException(name)
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
this.#drivers.set(name, { Driver: driver })
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* Get all the configuration of a channel.
|
|
107
|
-
*
|
|
108
|
-
* @param {string} channelName
|
|
109
|
-
* @return {any}
|
|
110
|
-
*/
|
|
111
|
-
static #getChannelConfig(channelName) {
|
|
112
|
-
if (channelName === 'default') {
|
|
113
|
-
channelName = Config.get('logging.default') || channelName
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
const channelConfig = Config.get(`logging.channels.${channelName}`)
|
|
117
|
-
|
|
118
|
-
if (!channelConfig) {
|
|
119
|
-
throw new NotImplementedConfigException(channelName)
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
if (!this.#drivers.has(channelConfig.driver)) {
|
|
123
|
-
throw new NotFoundDriverException(channelConfig.driver)
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
return channelConfig
|
|
127
|
-
}
|
|
128
|
-
}
|
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @athenna/logger
|
|
3
|
-
*
|
|
4
|
-
* (c) João Lenon <lenon@athenna.io>
|
|
5
|
-
*
|
|
6
|
-
* For the full copyright and license information, please view the LICENSE
|
|
7
|
-
* file that was distributed with this source code.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import { CliFormatter } from '#src/Formatters/CliFormatter'
|
|
11
|
-
import { JsonFormatter } from '#src/Formatters/JsonFormatter'
|
|
12
|
-
import { NoneFormatter } from '#src/Formatters/NoneFormatter'
|
|
13
|
-
import { SimpleFormatter } from '#src/Formatters/SimpleFormatter'
|
|
14
|
-
import { MessageFormatter } from '#src/Formatters/MessageFormatter'
|
|
15
|
-
import { RequestFormatter } from '#src/Formatters/RequestFormatter'
|
|
16
|
-
import { FormatterExistException } from '#src/Exceptions/FormatterExistException'
|
|
17
|
-
import { NotFoundFormatterException } from '#src/Exceptions/NotFoundFormatterException'
|
|
18
|
-
|
|
19
|
-
export class FormatterFactory {
|
|
20
|
-
/**
|
|
21
|
-
* Formatters of FormatterFactory.
|
|
22
|
-
*
|
|
23
|
-
* @type {Map<string, { Formatter: any }>}
|
|
24
|
-
*/
|
|
25
|
-
static formatters = new Map()
|
|
26
|
-
.set('cli', { Formatter: CliFormatter })
|
|
27
|
-
.set('json', { Formatter: JsonFormatter })
|
|
28
|
-
.set('none', { Formatter: NoneFormatter })
|
|
29
|
-
.set('simple', { Formatter: SimpleFormatter })
|
|
30
|
-
.set('message', { Formatter: MessageFormatter })
|
|
31
|
-
.set('request', { Formatter: RequestFormatter })
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Return an array with all available formatters.
|
|
35
|
-
*
|
|
36
|
-
* @return {any[]}
|
|
37
|
-
*/
|
|
38
|
-
static availableFormatters() {
|
|
39
|
-
const availableFormatters = []
|
|
40
|
-
|
|
41
|
-
for (const [key] of this.formatters.entries()) {
|
|
42
|
-
availableFormatters.push(key)
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
return availableFormatters
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Fabricate a new instance of a formatter.
|
|
50
|
-
*
|
|
51
|
-
* @param {string} formatterName
|
|
52
|
-
* @return {any}
|
|
53
|
-
*/
|
|
54
|
-
static fabricate(formatterName) {
|
|
55
|
-
const formatterObject = this.formatters.get(formatterName)
|
|
56
|
-
|
|
57
|
-
if (!formatterObject) {
|
|
58
|
-
throw new NotFoundFormatterException(formatterName)
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
return new formatterObject.Formatter()
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Creates a new formatter implementation.
|
|
66
|
-
*
|
|
67
|
-
* @param {string} name
|
|
68
|
-
* @param {() => any} formatter
|
|
69
|
-
*/
|
|
70
|
-
static createFormatter(name, formatter) {
|
|
71
|
-
if (this.formatters.has(name)) {
|
|
72
|
-
throw new FormatterExistException(name)
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
this.formatters.set(name, { Formatter: formatter })
|
|
76
|
-
}
|
|
77
|
-
}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @athenna/logger
|
|
3
|
-
*
|
|
4
|
-
* (c) João Lenon <lenon@athenna.io>
|
|
5
|
-
*
|
|
6
|
-
* For the full copyright and license information, please view the LICENSE
|
|
7
|
-
* file that was distributed with this source code.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import { Formatter } from '#src/Formatters/Formatter'
|
|
11
|
-
|
|
12
|
-
export class CliFormatter extends Formatter {
|
|
13
|
-
/**
|
|
14
|
-
* Format the message.
|
|
15
|
-
*
|
|
16
|
-
* @param {string} message
|
|
17
|
-
* @return {string}
|
|
18
|
-
*/
|
|
19
|
-
format(message) {
|
|
20
|
-
const level = this.cliLevel()
|
|
21
|
-
|
|
22
|
-
return this.clean(`${level} ${this.toString(message)}`)
|
|
23
|
-
}
|
|
24
|
-
}
|
|
@@ -1,266 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @athenna/logger
|
|
3
|
-
*
|
|
4
|
-
* (c) João Lenon <lenon@athenna.io>
|
|
5
|
-
*
|
|
6
|
-
* For the full copyright and license information, please view the LICENSE
|
|
7
|
-
* file that was distributed with this source code.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import rTracer from 'cls-rtracer'
|
|
11
|
-
|
|
12
|
-
import { hostname } from 'node:os'
|
|
13
|
-
import { Is } from '@athenna/common'
|
|
14
|
-
import { ColorHelper } from '#src/Helpers/ColorHelper'
|
|
15
|
-
|
|
16
|
-
export class Formatter {
|
|
17
|
-
/**
|
|
18
|
-
* Holds the configuration object of formatter.
|
|
19
|
-
*/
|
|
20
|
-
configs = {}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Creates a new instance of Formatter.
|
|
24
|
-
*
|
|
25
|
-
* @param {any} configs
|
|
26
|
-
* @return {Formatter}
|
|
27
|
-
*/
|
|
28
|
-
config(configs) {
|
|
29
|
-
this.configs = configs
|
|
30
|
-
|
|
31
|
-
return this
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Format the message.
|
|
36
|
-
*
|
|
37
|
-
* @param {string} message
|
|
38
|
-
* @return {string}
|
|
39
|
-
*/
|
|
40
|
-
format(message) {}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Create the PID for formatter.
|
|
44
|
-
*
|
|
45
|
-
* @return {string}
|
|
46
|
-
*/
|
|
47
|
-
pid() {
|
|
48
|
-
return process.pid.toString()
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Create the hostname for formatter.
|
|
53
|
-
*
|
|
54
|
-
* @return {string}
|
|
55
|
-
*/
|
|
56
|
-
hostname() {
|
|
57
|
-
return hostname()
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Get the level without any color or format.
|
|
62
|
-
*
|
|
63
|
-
* @return {string}
|
|
64
|
-
*/
|
|
65
|
-
level() {
|
|
66
|
-
return this.configs.level
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Get the trace id for formatter.
|
|
71
|
-
*
|
|
72
|
-
* @return {string | null}
|
|
73
|
-
*/
|
|
74
|
-
traceId() {
|
|
75
|
-
return rTracer.id() || null
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Create the timestamp for formatter.
|
|
80
|
-
*
|
|
81
|
-
* @return {string}
|
|
82
|
-
*/
|
|
83
|
-
timestamp() {
|
|
84
|
-
const localeStringOptions = {
|
|
85
|
-
year: 'numeric',
|
|
86
|
-
hour: 'numeric',
|
|
87
|
-
minute: 'numeric',
|
|
88
|
-
second: 'numeric',
|
|
89
|
-
day: '2-digit',
|
|
90
|
-
month: '2-digit',
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
return new Date(Date.now()).toLocaleString(undefined, localeStringOptions)
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Transform the message to string.
|
|
98
|
-
*
|
|
99
|
-
* @param message {string}
|
|
100
|
-
* @return {string}
|
|
101
|
-
*/
|
|
102
|
-
toString(message) {
|
|
103
|
-
if (Is.String(message)) {
|
|
104
|
-
return message
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
if (Is.Object(message)) {
|
|
108
|
-
message = JSON.stringify(message)
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
return `${message}`
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* Clean the message removing colors if clean
|
|
116
|
-
* option is true. If force is true, then colors
|
|
117
|
-
* will be removed even if configs clean option
|
|
118
|
-
* is false.
|
|
119
|
-
*
|
|
120
|
-
* @param message {string}
|
|
121
|
-
* @param [force] {boolean}
|
|
122
|
-
* @return {string}
|
|
123
|
-
*/
|
|
124
|
-
clean(message, force = false) {
|
|
125
|
-
if (this.configs.clean || force) {
|
|
126
|
-
return ColorHelper.removeColors(message)
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
return message
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
/**
|
|
133
|
-
* Apply all colors necessary to message.
|
|
134
|
-
*
|
|
135
|
-
* @param message {string}
|
|
136
|
-
* @return {string}
|
|
137
|
-
*/
|
|
138
|
-
applyColors(message) {
|
|
139
|
-
message = this.toString(message)
|
|
140
|
-
|
|
141
|
-
return this.applyColorsByChalk(this.applyColorsByLevel(message))
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Apply colors in message.
|
|
146
|
-
*
|
|
147
|
-
* @param message {string}
|
|
148
|
-
* @return {string}
|
|
149
|
-
*/
|
|
150
|
-
applyColorsByChalk(message) {
|
|
151
|
-
if (!this.configs.chalk) {
|
|
152
|
-
return message
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
return this.configs.chalk(message)
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* Apply colors in message by level.
|
|
160
|
-
*
|
|
161
|
-
* @param message {string}
|
|
162
|
-
* @return {string}
|
|
163
|
-
*/
|
|
164
|
-
applyColorsByLevel(message) {
|
|
165
|
-
const level = this.configs.level
|
|
166
|
-
|
|
167
|
-
return this.paintMessageByLevel(level, message)
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
/**
|
|
171
|
-
* Create the cli level string.
|
|
172
|
-
*
|
|
173
|
-
* @return {string}
|
|
174
|
-
*/
|
|
175
|
-
cliLevel() {
|
|
176
|
-
const level = this.configs.level
|
|
177
|
-
|
|
178
|
-
if (!ColorHelper[level]) {
|
|
179
|
-
return level
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
return ColorHelper[level].bold(`[ ${level} ]`)
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
/**
|
|
186
|
-
* Create the simple level string.
|
|
187
|
-
*
|
|
188
|
-
* @return {string}
|
|
189
|
-
*/
|
|
190
|
-
simpleLevel() {
|
|
191
|
-
const level = this.configs.level
|
|
192
|
-
|
|
193
|
-
if (!ColorHelper[level]) {
|
|
194
|
-
return level
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
return ColorHelper[level].bold(`[${level.toUpperCase()}]`)
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
* Create the message level emoji string.
|
|
202
|
-
*
|
|
203
|
-
* @return {string}
|
|
204
|
-
*/
|
|
205
|
-
messageLevel() {
|
|
206
|
-
const level = this.configs.level
|
|
207
|
-
|
|
208
|
-
return this.getEmojiByLevel(level, this.configs.customEmoji)
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* Get the emoji by level.
|
|
213
|
-
*
|
|
214
|
-
* @param {string} level
|
|
215
|
-
* @param {string} [customEmoji]
|
|
216
|
-
* @return {string}
|
|
217
|
-
*/
|
|
218
|
-
getEmojiByLevel(level, customEmoji) {
|
|
219
|
-
if (customEmoji) {
|
|
220
|
-
return customEmoji
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
const levelEmojis = {
|
|
224
|
-
trace: '\u{1F43E}',
|
|
225
|
-
debug: '\u{1F50E}',
|
|
226
|
-
info: '\u{2139}',
|
|
227
|
-
success: '\u{2705}',
|
|
228
|
-
warn: '\u{26A0}',
|
|
229
|
-
error: '\u{274C}',
|
|
230
|
-
fatal: '\u{1F6D1}',
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
if (!levelEmojis[level.toLowerCase()]) {
|
|
234
|
-
return ''
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
return levelEmojis[level.toLowerCase()]
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
/**
|
|
241
|
-
* Paint the message by level.
|
|
242
|
-
*
|
|
243
|
-
* @param {string} level
|
|
244
|
-
* @param {string} message
|
|
245
|
-
* @return {string}
|
|
246
|
-
*/
|
|
247
|
-
paintMessageByLevel(level, message) {
|
|
248
|
-
const levelLower = level.toLowerCase()
|
|
249
|
-
|
|
250
|
-
const levelColors = {
|
|
251
|
-
trace: ColorHelper.trace,
|
|
252
|
-
debug: ColorHelper.debug,
|
|
253
|
-
info: ColorHelper.info,
|
|
254
|
-
success: ColorHelper.success,
|
|
255
|
-
warn: ColorHelper.warn,
|
|
256
|
-
error: ColorHelper.error,
|
|
257
|
-
fatal: ColorHelper.fatal,
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
if (!levelColors[levelLower]) {
|
|
261
|
-
return message
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
return levelColors[levelLower](message)
|
|
265
|
-
}
|
|
266
|
-
}
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @athenna/logger
|
|
3
|
-
*
|
|
4
|
-
* (c) João Lenon <lenon@athenna.io>
|
|
5
|
-
*
|
|
6
|
-
* For the full copyright and license information, please view the LICENSE
|
|
7
|
-
* file that was distributed with this source code.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import { Is } from '@athenna/common'
|
|
11
|
-
import { Formatter } from '#src/Formatters/Formatter'
|
|
12
|
-
|
|
13
|
-
export class JsonFormatter extends Formatter {
|
|
14
|
-
/**
|
|
15
|
-
* Format the message.
|
|
16
|
-
*
|
|
17
|
-
* @param {any} message
|
|
18
|
-
* @return {string}
|
|
19
|
-
*/
|
|
20
|
-
format(message) {
|
|
21
|
-
const base = {
|
|
22
|
-
level: this.level(),
|
|
23
|
-
time: Date.now(),
|
|
24
|
-
pid: this.pid(),
|
|
25
|
-
hostname: this.hostname(),
|
|
26
|
-
traceId: this.traceId(),
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
if (Is.String(message)) {
|
|
30
|
-
base.msg = message
|
|
31
|
-
|
|
32
|
-
return JSON.stringify(base)
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
return JSON.stringify({ ...base, ...message })
|
|
36
|
-
}
|
|
37
|
-
}
|