@athenna/logger 1.1.8 → 1.1.9
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/LICENSE.md +3 -15
- package/README.md +6 -6
- package/package.json +84 -106
- package/src/Drivers/ConsoleDriver.js +43 -17
- package/src/Drivers/DebugDriver.js +45 -18
- package/src/Drivers/DiscordDriver.js +48 -25
- package/src/Drivers/FileDriver.js +51 -29
- package/src/Drivers/NullDriver.js +19 -7
- package/src/Drivers/PinoDriver.js +83 -61
- package/src/Drivers/SlackDriver.js +45 -22
- package/src/Drivers/TelegramDriver.js +44 -21
- package/src/Exceptions/DriverExistException.js +31 -0
- package/src/Exceptions/FormatterExistException.js +32 -0
- package/src/Exceptions/NotFoundChannelException.js +32 -0
- package/src/Exceptions/NotFoundDriverException.js +22 -10
- package/src/Exceptions/NotFoundFormatterException.js +22 -10
- package/src/Exceptions/OnlyPinoPrettyException.js +19 -10
- package/src/Facades/Log.js +9 -5
- package/src/Factories/DriverFactory.js +98 -56
- package/src/Factories/FormatterFactory.js +67 -37
- package/src/Formatters/CliFormatter.js +21 -22
- package/src/Formatters/JsonFormatter.js +15 -10
- package/src/Formatters/MessageFormatter.js +30 -33
- package/src/Formatters/NestFormatter.js +31 -22
- package/src/Formatters/NoneFormatter.js +21 -0
- package/src/Formatters/RequestFormatter.js +49 -37
- package/src/Formatters/SimpleFormatter.js +30 -33
- package/src/Helpers/ColorHelper.js +259 -0
- package/src/Helpers/FactoryHelper.js +121 -0
- package/src/Providers/LoggerProvider.js +13 -15
- package/src/index.d.ts +383 -0
- package/src/index.js +269 -0
- package/index.d.ts +0 -13
- package/index.js +0 -25
- package/src/Contracts/DefaultDriverConfigs.d.ts +0 -4
- package/src/Contracts/DefaultDriverConfigs.js +0 -2
- package/src/Contracts/DriverContract.d.ts +0 -13
- package/src/Contracts/DriverContract.js +0 -10
- package/src/Contracts/FormatterContract.d.ts +0 -11
- package/src/Contracts/FormatterContract.js +0 -10
- package/src/Contracts/LevelTypes.d.ts +0 -1
- package/src/Contracts/LevelTypes.js +0 -2
- package/src/Drivers/ConsoleDriver.d.ts +0 -19
- package/src/Drivers/DebugDriver.d.ts +0 -19
- package/src/Drivers/DiscordDriver.d.ts +0 -20
- package/src/Drivers/FileDriver.d.ts +0 -19
- package/src/Drivers/NullDriver.d.ts +0 -13
- package/src/Drivers/PinoDriver.d.ts +0 -20
- package/src/Drivers/SlackDriver.d.ts +0 -19
- package/src/Drivers/TelegramDriver.d.ts +0 -21
- package/src/Exceptions/ChannelNotConfiguredException.d.ts +0 -12
- package/src/Exceptions/ChannelNotConfiguredException.js +0 -19
- package/src/Exceptions/DriverAlreadyExistException.d.ts +0 -12
- package/src/Exceptions/DriverAlreadyExistException.js +0 -19
- package/src/Exceptions/FormatterAlreadyExistException.d.ts +0 -12
- package/src/Exceptions/FormatterAlreadyExistException.js +0 -19
- package/src/Exceptions/NotFoundDriverException.d.ts +0 -12
- package/src/Exceptions/NotFoundFormatterException.d.ts +0 -12
- package/src/Exceptions/OnlyPinoPrettyException.d.ts +0 -12
- package/src/Facades/Log.d.ts +0 -10
- package/src/Factories/DriverFactory.d.ts +0 -19
- package/src/Factories/FormatterFactory.d.ts +0 -18
- package/src/Formatters/CliFormatter.d.ts +0 -19
- package/src/Formatters/JsonFormatter.d.ts +0 -16
- package/src/Formatters/MessageFormatter.d.ts +0 -20
- package/src/Formatters/NestFormatter.d.ts +0 -19
- package/src/Formatters/RequestFormatter.d.ts +0 -17
- package/src/Formatters/SimpleFormatter.d.ts +0 -21
- package/src/Logger.d.ts +0 -110
- package/src/Logger.js +0 -216
- package/src/Providers/LoggerProvider.d.ts +0 -17
- package/src/Utils/Color.d.ts +0 -48
- package/src/Utils/Color.js +0 -93
- package/src/Utils/getTimestamp.d.ts +0 -9
- package/src/Utils/getTimestamp.js +0 -23
- package/src/Utils/groupConfigs.d.ts +0 -9
- package/src/Utils/groupConfigs.js +0 -18
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* @athenna/logger
|
|
4
3
|
*
|
|
@@ -7,63 +6,106 @@
|
|
|
7
6
|
* For the full copyright and license information, please view the LICENSE
|
|
8
7
|
* file that was distributed with this source code.
|
|
9
8
|
*/
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
9
|
+
|
|
10
|
+
import { Config } from '@secjs/utils'
|
|
11
|
+
|
|
12
|
+
import { FileDriver } from '#src/Drivers/FileDriver'
|
|
13
|
+
import { NullDriver } from '#src/Drivers/NullDriver'
|
|
14
|
+
import { PinoDriver } from '#src/Drivers/PinoDriver'
|
|
15
|
+
import { DebugDriver } from '#src/Drivers/DebugDriver'
|
|
16
|
+
import { SlackDriver } from '#src/Drivers/SlackDriver'
|
|
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 { NotFoundChannelException } from '#src/Exceptions/NotFoundChannelException'
|
|
23
|
+
|
|
24
|
+
export class DriverFactory {
|
|
25
|
+
/**
|
|
26
|
+
* Drivers of DriverFactory.
|
|
27
|
+
*
|
|
28
|
+
* @type {Map<string, { Driver: any }>}
|
|
29
|
+
*/
|
|
30
|
+
static drivers = new Map()
|
|
31
|
+
.set('file', { Driver: FileDriver })
|
|
32
|
+
.set('null', { Driver: NullDriver })
|
|
33
|
+
.set('pino', { Driver: PinoDriver })
|
|
34
|
+
.set('slack', { Driver: SlackDriver })
|
|
35
|
+
.set('debug', { Driver: DebugDriver })
|
|
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.entries()) {
|
|
49
|
+
availableDrivers.push(key)
|
|
31
50
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
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
|
+
if (channelName === 'default') {
|
|
65
|
+
channelName = Config.get('logging.default')
|
|
42
66
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
67
|
+
|
|
68
|
+
const channelConfig = this.#getChannelConfig(channelName)
|
|
69
|
+
const driverObject = this.drivers.get(channelConfig.driver)
|
|
70
|
+
|
|
71
|
+
if (!driverObject) {
|
|
72
|
+
throw new NotFoundDriverException(channelConfig.driver)
|
|
48
73
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
74
|
+
|
|
75
|
+
return new driverObject.Driver(channelName, runtimeConfig)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Creates a new driver implementation.
|
|
80
|
+
*
|
|
81
|
+
* @param {string} name
|
|
82
|
+
* @param {(channel: string, configs?: any) => any} driver
|
|
83
|
+
*/
|
|
84
|
+
static createDriver(name, driver) {
|
|
85
|
+
if (this.drivers.has(name)) {
|
|
86
|
+
throw new DriverExistException(name)
|
|
58
87
|
}
|
|
88
|
+
|
|
89
|
+
this.drivers.set(name, { Driver: driver })
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Get all the configuration of a channel.
|
|
94
|
+
*
|
|
95
|
+
* @param {string} channelName
|
|
96
|
+
* @return {any}
|
|
97
|
+
*/
|
|
98
|
+
static #getChannelConfig(channelName) {
|
|
99
|
+
const channelConfig = Config.get(`logging.channels.${channelName}`)
|
|
100
|
+
|
|
101
|
+
if (!channelConfig) {
|
|
102
|
+
throw new NotFoundChannelException(channelName)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (!this.drivers.has(channelConfig.driver)) {
|
|
106
|
+
throw new NotFoundDriverException(channelConfig.driver)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return channelConfig
|
|
110
|
+
}
|
|
59
111
|
}
|
|
60
|
-
exports.DriverFactory = DriverFactory;
|
|
61
|
-
DriverFactory.drivers = new Map()
|
|
62
|
-
.set('file', { Driver: FileDriver_1.FileDriver })
|
|
63
|
-
.set('null', { Driver: NullDriver_1.NullDriver })
|
|
64
|
-
.set('pino', { Driver: PinoDriver_1.PinoDriver })
|
|
65
|
-
.set('slack', { Driver: SlackDriver_1.SlackDriver })
|
|
66
|
-
.set('debug', { Driver: DebugDriver_1.DebugDriver })
|
|
67
|
-
.set('console', { Driver: ConsoleDriver_1.ConsoleDriver })
|
|
68
|
-
.set('discord', { Driver: DiscordDriver_1.DiscordDriver })
|
|
69
|
-
.set('telegram', { Driver: TelegramDriver_1.TelegramDriver });
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* @athenna/logger
|
|
4
3
|
*
|
|
@@ -7,43 +6,74 @@
|
|
|
7
6
|
* For the full copyright and license information, please view the LICENSE
|
|
8
7
|
* file that was distributed with this source code.
|
|
9
8
|
*/
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
9
|
+
|
|
10
|
+
import { CliFormatter } from '#src/Formatters/CliFormatter'
|
|
11
|
+
import { NestFormatter } from '#src/Formatters/NestFormatter'
|
|
12
|
+
import { JsonFormatter } from '#src/Formatters/JsonFormatter'
|
|
13
|
+
import { NoneFormatter } from '#src/Formatters/NoneFormatter'
|
|
14
|
+
import { SimpleFormatter } from '#src/Formatters/SimpleFormatter'
|
|
15
|
+
import { MessageFormatter } from '#src/Formatters/MessageFormatter'
|
|
16
|
+
import { RequestFormatter } from '#src/Formatters/RequestFormatter'
|
|
17
|
+
import { FormatterExistException } from '#src/Exceptions/FormatterExistException'
|
|
18
|
+
import { NotFoundFormatterException } from '#src/Exceptions/NotFoundFormatterException'
|
|
19
|
+
|
|
20
|
+
export class FormatterFactory {
|
|
21
|
+
/**
|
|
22
|
+
* Formatters of FormatterFactory.
|
|
23
|
+
*
|
|
24
|
+
* @type {Map<string, { Formatter: any }>}
|
|
25
|
+
*/
|
|
26
|
+
static formatters = new Map()
|
|
27
|
+
.set('cli', { Formatter: CliFormatter })
|
|
28
|
+
.set('nest', { Formatter: NestFormatter })
|
|
29
|
+
.set('json', { Formatter: JsonFormatter })
|
|
30
|
+
.set('none', { Formatter: NoneFormatter })
|
|
31
|
+
.set('simple', { Formatter: SimpleFormatter })
|
|
32
|
+
.set('message', { Formatter: MessageFormatter })
|
|
33
|
+
.set('request', { Formatter: RequestFormatter })
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Return an array with all available formatters.
|
|
37
|
+
*
|
|
38
|
+
* @return {any[]}
|
|
39
|
+
*/
|
|
40
|
+
static availableFormatters() {
|
|
41
|
+
const availableFormatters = []
|
|
42
|
+
|
|
43
|
+
for (const [key] of this.formatters.entries()) {
|
|
44
|
+
availableFormatters.push(key)
|
|
27
45
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
46
|
+
|
|
47
|
+
return availableFormatters
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Fabricate a new instance of a formatter.
|
|
52
|
+
*
|
|
53
|
+
* @param {string} formatterName
|
|
54
|
+
* @return {any}
|
|
55
|
+
*/
|
|
56
|
+
static fabricate(formatterName) {
|
|
57
|
+
const formatterObject = this.formatters.get(formatterName)
|
|
58
|
+
|
|
59
|
+
if (!formatterObject) {
|
|
60
|
+
throw new NotFoundFormatterException(formatterName)
|
|
34
61
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
62
|
+
|
|
63
|
+
return new formatterObject.Formatter()
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Creates a new formatter implementation.
|
|
68
|
+
*
|
|
69
|
+
* @param {string} name
|
|
70
|
+
* @param {() => any} formatter
|
|
71
|
+
*/
|
|
72
|
+
static createFormatter(name, formatter) {
|
|
73
|
+
if (this.formatters.has(name)) {
|
|
74
|
+
throw new FormatterExistException(name)
|
|
40
75
|
}
|
|
76
|
+
|
|
77
|
+
this.formatters.set(name, { Formatter: formatter })
|
|
78
|
+
}
|
|
41
79
|
}
|
|
42
|
-
exports.FormatterFactory = FormatterFactory;
|
|
43
|
-
FormatterFactory.formatters = new Map()
|
|
44
|
-
.set('cli', { Formatter: CliFormatter_1.CliFormatter })
|
|
45
|
-
.set('nest', { Formatter: NestFormatter_1.NestFormatter })
|
|
46
|
-
.set('json', { Formatter: JsonFormatter_1.JsonFormatter })
|
|
47
|
-
.set('simple', { Formatter: SimpleFormatter_1.SimpleFormatter })
|
|
48
|
-
.set('message', { Formatter: MessageFormatter_1.MessageFormatter })
|
|
49
|
-
.set('request', { Formatter: RequestFormatter_1.RequestFormatter });
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* @athenna/logger
|
|
4
3
|
*
|
|
@@ -7,25 +6,25 @@
|
|
|
7
6
|
* For the full copyright and license information, please view the LICENSE
|
|
8
7
|
* file that was distributed with this source code.
|
|
9
8
|
*/
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
class CliFormatter {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
9
|
+
|
|
10
|
+
import { FactoryHelper } from '#src/index'
|
|
11
|
+
|
|
12
|
+
export class CliFormatter {
|
|
13
|
+
/**
|
|
14
|
+
* Format the message.
|
|
15
|
+
*
|
|
16
|
+
* @param {string} message
|
|
17
|
+
* @param {{ level: 'info'|'INFO'|'debug'|'DEBUG'|'warn'|'WARN'|'error'|'ERROR'|'success'|'SUCCESS', chalk: import('chalk').ChalkInstance }} options
|
|
18
|
+
* @return {string}
|
|
19
|
+
*/
|
|
20
|
+
format(message, options) {
|
|
21
|
+
options.level = options.level.toLowerCase()
|
|
22
|
+
|
|
23
|
+
const level = FactoryHelper.paintByLevel(
|
|
24
|
+
options.level,
|
|
25
|
+
`[ ${options.level} ]`,
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
return `${level} ${message}`
|
|
29
|
+
}
|
|
30
30
|
}
|
|
31
|
-
exports.CliFormatter = CliFormatter;
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* @athenna/logger
|
|
4
3
|
*
|
|
@@ -7,13 +6,19 @@
|
|
|
7
6
|
* For the full copyright and license information, please view the LICENSE
|
|
8
7
|
* file that was distributed with this source code.
|
|
9
8
|
*/
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
class JsonFormatter {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
9
|
+
|
|
10
|
+
import { ColorHelper } from '#src/index'
|
|
11
|
+
|
|
12
|
+
export class JsonFormatter {
|
|
13
|
+
/**
|
|
14
|
+
*
|
|
15
|
+
* @param {Record<any, any>} message
|
|
16
|
+
* @param {{ chalk: import('chalk').ChalkInstance }} options
|
|
17
|
+
* @return {string}
|
|
18
|
+
*/
|
|
19
|
+
format(message, options) {
|
|
20
|
+
const jsonSpaced = JSON.stringify(message, null, 2)
|
|
21
|
+
|
|
22
|
+
return `${ColorHelper.bold('JSON:')} ${options.chalk(jsonSpaced)}`
|
|
23
|
+
}
|
|
18
24
|
}
|
|
19
|
-
exports.JsonFormatter = JsonFormatter;
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/* eslint-disable prettier/prettier */
|
|
3
2
|
/**
|
|
4
3
|
* @athenna/logger
|
|
@@ -8,36 +7,34 @@
|
|
|
8
7
|
* For the full copyright and license information, please view the LICENSE
|
|
9
8
|
* file that was distributed with this source code.
|
|
10
9
|
*/
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
class MessageFormatter {
|
|
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
|
-
}
|
|
10
|
+
|
|
11
|
+
import { FactoryHelper } from '#src/index'
|
|
12
|
+
|
|
13
|
+
export class MessageFormatter {
|
|
14
|
+
/**
|
|
15
|
+
* The last timestamp.
|
|
16
|
+
*
|
|
17
|
+
* @type {number}
|
|
18
|
+
*/
|
|
19
|
+
#lastTimestamp
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Format the message.
|
|
23
|
+
*
|
|
24
|
+
* @param {string} message
|
|
25
|
+
* @param {{ level: 'info'|'INFO'|'debug'|'DEBUG'|'warn'|'WARN'|'error'|'ERROR'|'success'|'SUCCESS', customEmoji: string }} options
|
|
26
|
+
* @return {string}
|
|
27
|
+
*/
|
|
28
|
+
format(message, options) {
|
|
29
|
+
const timestampDiff = FactoryHelper.getTimestampDiff(this.#lastTimestamp)
|
|
30
|
+
|
|
31
|
+
this.#lastTimestamp = Date.now()
|
|
32
|
+
|
|
33
|
+
const level = FactoryHelper.getEmojiByLevel(
|
|
34
|
+
options.level,
|
|
35
|
+
options.customEmoji,
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
return `${level} ${message}${timestampDiff}`
|
|
39
|
+
}
|
|
42
40
|
}
|
|
43
|
-
exports.MessageFormatter = MessageFormatter;
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* @athenna/logger
|
|
4
3
|
*
|
|
@@ -7,25 +6,35 @@
|
|
|
7
6
|
* For the full copyright and license information, please view the LICENSE
|
|
8
7
|
* file that was distributed with this source code.
|
|
9
8
|
*/
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
9
|
+
|
|
10
|
+
import { ColorHelper, FactoryHelper } from '#src/index'
|
|
11
|
+
|
|
12
|
+
export class NestFormatter {
|
|
13
|
+
/**
|
|
14
|
+
* The last timestamp.
|
|
15
|
+
*
|
|
16
|
+
* @type {number}
|
|
17
|
+
*/
|
|
18
|
+
#lastTimestamp
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Format the message.
|
|
22
|
+
*
|
|
23
|
+
* @param {string} message
|
|
24
|
+
* @param {{ context: string, chalk: import('chalk').ChalkInstance }} options
|
|
25
|
+
* @return {string}
|
|
26
|
+
*/
|
|
27
|
+
format(message, options) {
|
|
28
|
+
const timestampDiff = FactoryHelper.getTimestampDiff(this.#lastTimestamp)
|
|
29
|
+
|
|
30
|
+
this.#lastTimestamp = Date.now()
|
|
31
|
+
|
|
32
|
+
const pid = ColorHelper.yellow(`[Athenna] - PID: ${process.pid}`)
|
|
33
|
+
const timestamp = FactoryHelper.getTimestamp()
|
|
34
|
+
const messageCtx = ColorHelper.yellow(`[${options.context}] `)
|
|
35
|
+
|
|
36
|
+
return `${pid} - ${timestamp} ${messageCtx}${options.chalk(
|
|
37
|
+
message,
|
|
38
|
+
)}${timestampDiff}`
|
|
39
|
+
}
|
|
30
40
|
}
|
|
31
|
-
exports.NestFormatter = NestFormatter;
|
|
@@ -0,0 +1,21 @@
|
|
|
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
|
+
export class NoneFormatter {
|
|
11
|
+
/**
|
|
12
|
+
* Format the message.
|
|
13
|
+
*
|
|
14
|
+
* @param {string} message
|
|
15
|
+
* @param {any} [_options]
|
|
16
|
+
* @return {string}
|
|
17
|
+
*/
|
|
18
|
+
format(message, _options) {
|
|
19
|
+
return message
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* @athenna/logger
|
|
4
3
|
*
|
|
@@ -7,41 +6,54 @@
|
|
|
7
6
|
* For the full copyright and license information, please view the LICENSE
|
|
8
7
|
* file that was distributed with this source code.
|
|
9
8
|
*/
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
class RequestFormatter {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
};
|
|
32
|
-
const request = {
|
|
33
|
-
url: ctx.request.hostUrl,
|
|
34
|
-
method: ctx.request.method,
|
|
35
|
-
body: ctx.request.body,
|
|
36
|
-
params: ctx.request.params,
|
|
37
|
-
queries: ctx.request.queries,
|
|
38
|
-
headers: ctx.request.headers,
|
|
39
|
-
};
|
|
40
|
-
const response = {
|
|
41
|
-
body: ctx.body,
|
|
42
|
-
headers: ctx.headers,
|
|
43
|
-
};
|
|
44
|
-
return JSON.stringify({ request, response, metadata });
|
|
9
|
+
|
|
10
|
+
import { ColorHelper } from '#src/index'
|
|
11
|
+
|
|
12
|
+
export class RequestFormatter {
|
|
13
|
+
/**
|
|
14
|
+
* Format the message.
|
|
15
|
+
*
|
|
16
|
+
* @param {any} ctx
|
|
17
|
+
* @param {{ asJson: boolean, chalk: import('chalk').ChalkInstance }} options
|
|
18
|
+
* @return {string}
|
|
19
|
+
*/
|
|
20
|
+
format(ctx, options) {
|
|
21
|
+
const ip = ctx.request.ip
|
|
22
|
+
const status = ctx.status
|
|
23
|
+
const responseTimeMs = `${Math.round(ctx.responseTime)}ms`
|
|
24
|
+
const methodAndUrl = ColorHelper[ctx.request.method](
|
|
25
|
+
`${ctx.request.method}::${ctx.request.baseUrl}`,
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
if (!options.asJson) {
|
|
29
|
+
return `(${ip}) - [${status}] ${methodAndUrl} ${responseTimeMs}`
|
|
45
30
|
}
|
|
31
|
+
|
|
32
|
+
const metadata = {
|
|
33
|
+
method: ctx.request.method,
|
|
34
|
+
duration: responseTimeMs,
|
|
35
|
+
status: status <= 399 ? 'SUCCESS' : 'ERROR',
|
|
36
|
+
statusCode: status,
|
|
37
|
+
url: ctx.request.hostUrl,
|
|
38
|
+
path: ctx.request.baseUrl,
|
|
39
|
+
createdAt: Date.now(),
|
|
40
|
+
data: ctx.data,
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const request = {
|
|
44
|
+
url: ctx.request.hostUrl,
|
|
45
|
+
method: ctx.request.method,
|
|
46
|
+
body: ctx.request.body,
|
|
47
|
+
params: ctx.request.params,
|
|
48
|
+
queries: ctx.request.queries,
|
|
49
|
+
headers: ctx.request.headers,
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const response = {
|
|
53
|
+
body: ctx.body,
|
|
54
|
+
headers: ctx.headers,
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return JSON.stringify({ request, response, metadata })
|
|
58
|
+
}
|
|
46
59
|
}
|
|
47
|
-
exports.RequestFormatter = RequestFormatter;
|