@athenna/logger 1.1.7 → 1.2.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.
Files changed (66) hide show
  1. package/LICENSE.md +3 -15
  2. package/README.md +6 -6
  3. package/package.json +88 -101
  4. package/src/Drivers/ConsoleDriver.js +43 -22
  5. package/src/Drivers/DebugDriver.js +45 -23
  6. package/src/Drivers/DiscordDriver.js +57 -0
  7. package/src/Drivers/FileDriver.js +51 -34
  8. package/src/Drivers/NullDriver.js +28 -0
  9. package/src/Drivers/PinoDriver.js +94 -0
  10. package/src/Drivers/SlackDriver.js +54 -0
  11. package/src/Drivers/TelegramDriver.js +53 -0
  12. package/src/Exceptions/DriverExistException.js +31 -0
  13. package/src/Exceptions/FormatterExistException.js +32 -0
  14. package/src/Exceptions/NotFoundChannelException.js +32 -0
  15. package/src/Exceptions/NotFoundDriverException.js +22 -10
  16. package/src/Exceptions/NotFoundFormatterException.js +22 -10
  17. package/src/Exceptions/OnlyPinoPrettyException.js +28 -0
  18. package/src/Facades/Log.js +9 -5
  19. package/src/Factories/DriverFactory.js +98 -46
  20. package/src/Factories/FormatterFactory.js +67 -35
  21. package/src/Formatters/CliFormatter.js +21 -20
  22. package/src/Formatters/JsonFormatter.js +15 -10
  23. package/src/Formatters/MessageFormatter.js +40 -0
  24. package/src/Formatters/NestFormatter.js +31 -22
  25. package/src/Formatters/NoneFormatter.js +21 -0
  26. package/src/Formatters/RequestFormatter.js +49 -37
  27. package/src/Formatters/SimpleFormatter.js +30 -31
  28. package/src/Helpers/ColorHelper.js +259 -0
  29. package/src/Helpers/FactoryHelper.js +121 -0
  30. package/src/Providers/LoggerProvider.js +13 -15
  31. package/src/index.d.ts +383 -0
  32. package/src/index.js +269 -0
  33. package/index.d.ts +0 -13
  34. package/index.js +0 -25
  35. package/src/Contracts/DriverContract.d.ts +0 -11
  36. package/src/Contracts/DriverContract.js +0 -10
  37. package/src/Contracts/FormatterContract.d.ts +0 -11
  38. package/src/Contracts/FormatterContract.js +0 -10
  39. package/src/Contracts/LevelTypes.d.ts +0 -1
  40. package/src/Contracts/LevelTypes.js +0 -2
  41. package/src/Drivers/ConsoleDriver.d.ts +0 -25
  42. package/src/Drivers/DebugDriver.d.ts +0 -25
  43. package/src/Drivers/FileDriver.d.ts +0 -23
  44. package/src/Exceptions/ChannelNotConfiguredException.d.ts +0 -12
  45. package/src/Exceptions/ChannelNotConfiguredException.js +0 -19
  46. package/src/Exceptions/DriverAlreadyExistException.d.ts +0 -12
  47. package/src/Exceptions/DriverAlreadyExistException.js +0 -19
  48. package/src/Exceptions/FormatterAlreadyExistException.d.ts +0 -12
  49. package/src/Exceptions/FormatterAlreadyExistException.js +0 -19
  50. package/src/Exceptions/NotFoundDriverException.d.ts +0 -12
  51. package/src/Exceptions/NotFoundFormatterException.d.ts +0 -12
  52. package/src/Facades/Log.d.ts +0 -10
  53. package/src/Factories/DriverFactory.d.ts +0 -19
  54. package/src/Factories/FormatterFactory.d.ts +0 -18
  55. package/src/Formatters/CliFormatter.d.ts +0 -19
  56. package/src/Formatters/JsonFormatter.d.ts +0 -16
  57. package/src/Formatters/NestFormatter.d.ts +0 -19
  58. package/src/Formatters/RequestFormatter.d.ts +0 -17
  59. package/src/Formatters/SimpleFormatter.d.ts +0 -21
  60. package/src/Logger.d.ts +0 -110
  61. package/src/Logger.js +0 -223
  62. package/src/Providers/LoggerProvider.d.ts +0 -17
  63. package/src/Utils/Color.d.ts +0 -48
  64. package/src/Utils/Color.js +0 -93
  65. package/src/Utils/getTimestamp.d.ts +0 -9
  66. package/src/Utils/getTimestamp.js +0 -23
package/src/Logger.js DELETED
@@ -1,223 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.Logger = void 0;
7
- const chalk_1 = __importDefault(require("chalk"));
8
- const utils_1 = require("@secjs/utils");
9
- const Color_1 = require("./Utils/Color");
10
- const DriverFactory_1 = require("./Factories/DriverFactory");
11
- const FormatterFactory_1 = require("./Factories/FormatterFactory");
12
- class Logger {
13
- /**
14
- * Creates a new instance of Logger.
15
- *
16
- * @return {Logger}
17
- */
18
- constructor() {
19
- /**
20
- * Runtime configurations to be used inside the Drivers and Formatters.
21
- * @private
22
- */
23
- this.runtimeConfig = {};
24
- /**
25
- * The log channel selected with driver and formatter configurations.
26
- * @private
27
- */
28
- this.channelName = 'default';
29
- this.driver = DriverFactory_1.DriverFactory.fabricate(this.channelName, this.runtimeConfig);
30
- }
31
- /**
32
- * Return all drivers available.
33
- */
34
- static get drivers() {
35
- return DriverFactory_1.DriverFactory.availableDrivers();
36
- }
37
- /**
38
- * Return all formatters available.
39
- */
40
- static get formatters() {
41
- return FormatterFactory_1.FormatterFactory.availableFormatters();
42
- }
43
- /**
44
- * Builds a new driver to use within Logger class.
45
- *
46
- * @param name
47
- * @param driver
48
- */
49
- static buildDriver(name, driver) {
50
- DriverFactory_1.DriverFactory.createDriver(name, driver);
51
- }
52
- /**
53
- * Builds a new formatter to use within Logger class.
54
- *
55
- * @param name
56
- * @param formatter
57
- */
58
- static buildFormatter(name, formatter) {
59
- FormatterFactory_1.FormatterFactory.createFormatter(name, formatter);
60
- }
61
- /**
62
- * Applies the log engine to execute chalk methods of string.
63
- *
64
- * @param content
65
- * @private
66
- */
67
- static applyLogEngine(content) {
68
- if (utils_1.Is.String(content)) {
69
- const matches = content.match(/\({(.*?)} (.*?)\)/);
70
- if (matches) {
71
- const chalkMethodsString = matches[1].replace(/\s/g, '');
72
- const chalkMethodsArray = chalkMethodsString.split(',');
73
- const message = matches[2];
74
- let chalk = chalk_1.default;
75
- chalkMethodsArray.forEach(chalkMethod => {
76
- if (!chalk[chalkMethod])
77
- return;
78
- chalk = chalk[chalkMethod];
79
- });
80
- content = content
81
- .replace(`({${matches[1]}} `, '')
82
- .replace(`({${matches[1]}}`, '')
83
- .replace(`${matches[2]})`, chalk(message));
84
- }
85
- return content;
86
- }
87
- return content;
88
- }
89
- /**
90
- * Change the log channel.
91
- *
92
- * @param channel
93
- * @param runtimeConfig
94
- */
95
- channel(channel, runtimeConfig) {
96
- if (runtimeConfig)
97
- this.runtimeConfig = runtimeConfig;
98
- this.driver = DriverFactory_1.DriverFactory.fabricate(channel, this.runtimeConfig);
99
- this.channelName = channel;
100
- return this;
101
- }
102
- /**
103
- * Creates a log of type log in channel.
104
- * @param message
105
- * @param options
106
- */
107
- log(message, options = {}) {
108
- options = this.createOptions(options, {});
109
- message = Logger.applyLogEngine(message);
110
- return this.driver.transport(message, options);
111
- }
112
- /**
113
- * Creates a log of type info in channel.
114
- *
115
- * @param message
116
- * @param options
117
- */
118
- info(message, options = {}) {
119
- options = this.createOptions(options, {
120
- formatterConfig: {
121
- level: 'INFO',
122
- chalk: Color_1.Color.cyan,
123
- },
124
- });
125
- message = Logger.applyLogEngine(message);
126
- return this.driver.transport(message, options);
127
- }
128
- /**
129
- * Creates a log of type warn in channel.
130
- *
131
- * @param message
132
- * @param options
133
- */
134
- warn(message, options = {}) {
135
- options = this.createOptions(options, {
136
- streamType: 'stdout',
137
- formatterConfig: {
138
- level: 'WARN',
139
- chalk: Color_1.Color.orange,
140
- },
141
- });
142
- message = Logger.applyLogEngine(message);
143
- return this.driver.transport(message, options);
144
- }
145
- /**
146
- * Creates a log of type error in channel.
147
- *
148
- * @param message
149
- * @param options
150
- */
151
- error(message, options = {}) {
152
- options = this.createOptions(options, {
153
- streamType: 'stdout',
154
- formatterConfig: {
155
- level: 'ERROR',
156
- chalk: Color_1.Color.red,
157
- },
158
- });
159
- message = Logger.applyLogEngine(message);
160
- return this.driver.transport(message, options);
161
- }
162
- /**
163
- * Creates a log of type debug in channel.
164
- *
165
- * @param message
166
- * @param options
167
- */
168
- debug(message, options = {}) {
169
- options = this.createOptions(options, {
170
- streamType: 'stdout',
171
- formatterConfig: {
172
- level: 'DEBUG',
173
- chalk: Color_1.Color.purple,
174
- },
175
- });
176
- message = Logger.applyLogEngine(message);
177
- return this.driver.transport(message, options);
178
- }
179
- /**
180
- * Creates a log of type success in channel.
181
- *
182
- * @param message
183
- * @param options
184
- */
185
- success(message, options = {}) {
186
- options = this.createOptions(options, {
187
- streamType: 'stdout',
188
- formatterConfig: {
189
- level: 'SUCCESS',
190
- chalk: Color_1.Color.green,
191
- },
192
- });
193
- message = Logger.applyLogEngine(message);
194
- return this.driver.transport(message, options);
195
- }
196
- /**
197
- * Create options concatenating client options with default options.
198
- *
199
- * @param options
200
- * @param defaultValues
201
- * @private
202
- */
203
- createOptions(options, defaultValues) {
204
- let formatterConfig = Object.assign({}, {
205
- ...defaultValues.formatterConfig,
206
- }, options.formatterConfig);
207
- if (this.runtimeConfig.formatterConfig) {
208
- formatterConfig = {
209
- ...this.runtimeConfig.formatterConfig,
210
- ...formatterConfig,
211
- };
212
- }
213
- options = Object.assign({}, {
214
- streamType: 'stdout',
215
- }, options);
216
- options = {
217
- ...options,
218
- formatterConfig,
219
- };
220
- return options;
221
- }
222
- }
223
- exports.Logger = Logger;
@@ -1,17 +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
- import { ServiceProvider } from '@athenna/ioc';
10
- export declare class LoggerProvider extends ServiceProvider {
11
- /**
12
- * Register any application services.
13
- *
14
- * @return void
15
- */
16
- register(): void;
17
- }
@@ -1,48 +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
- import chalk, { Chalk } from 'chalk';
10
- declare type Methods = 'GET' | 'POST' | 'PUT' | 'DELETE';
11
- export declare class Color {
12
- static chalk: chalk.Chalk & chalk.ChalkFunction & {
13
- supportsColor: false | chalk.ColorSupport;
14
- Level: chalk.Level;
15
- Color: ("black" | "red" | "green" | "yellow" | "blue" | "magenta" | "cyan" | "white" | "gray" | "grey" | "blackBright" | "redBright" | "greenBright" | "yellowBright" | "blueBright" | "magentaBright" | "cyanBright" | "whiteBright") | ("bgBlack" | "bgRed" | "bgGreen" | "bgYellow" | "bgBlue" | "bgMagenta" | "bgCyan" | "bgWhite" | "bgGray" | "bgGrey" | "bgBlackBright" | "bgRedBright" | "bgGreenBright" | "bgYellowBright" | "bgBlueBright" | "bgMagentaBright" | "bgCyanBright" | "bgWhiteBright");
16
- ForegroundColor: "black" | "red" | "green" | "yellow" | "blue" | "magenta" | "cyan" | "white" | "gray" | "grey" | "blackBright" | "redBright" | "greenBright" | "yellowBright" | "blueBright" | "magentaBright" | "cyanBright" | "whiteBright";
17
- BackgroundColor: "bgBlack" | "bgRed" | "bgGreen" | "bgYellow" | "bgBlue" | "bgMagenta" | "bgCyan" | "bgWhite" | "bgGray" | "bgGrey" | "bgBlackBright" | "bgRedBright" | "bgGreenBright" | "bgYellowBright" | "bgBlueBright" | "bgMagentaBright" | "bgCyanBright" | "bgWhiteBright";
18
- Modifiers: "reset" | "bold" | "dim" | "italic" | "underline" | "inverse" | "hidden" | "strikethrough" | "visible";
19
- stderr: chalk.Chalk & {
20
- supportsColor: false | chalk.ColorSupport;
21
- };
22
- };
23
- static get bold(): Chalk;
24
- static get purple(): Chalk;
25
- static get darkPurple(): Chalk;
26
- static get yellow(): Chalk;
27
- static get cyan(): Chalk;
28
- static get white(): Chalk;
29
- static get orange(): Chalk;
30
- static get green(): Chalk;
31
- static get darkGreen(): Chalk;
32
- static get red(): Chalk;
33
- static get info(): any;
34
- static get log(): any;
35
- static get debug(): any;
36
- static get error(): any;
37
- static get warning(): any;
38
- static get GET(): any;
39
- static get HEAD(): any;
40
- static get PUT(): any;
41
- static get PATCH(): any;
42
- static get POST(): any;
43
- static get DELETE(): any;
44
- static get OPTIONS(): any;
45
- static removeColors(string: string): any;
46
- static httpMethod(method: Methods): any;
47
- }
48
- export {};
@@ -1,93 +0,0 @@
1
- "use strict";
2
- /**
3
- * @athenna/logger
4
- *
5
- * (c) João Lenon <lenon@athenna.io>
6
- *
7
- * For the full copyright and license information, please view the LICENSE
8
- * file that was distributed with this source code.
9
- */
10
- var __importDefault = (this && this.__importDefault) || function (mod) {
11
- return (mod && mod.__esModule) ? mod : { "default": mod };
12
- };
13
- Object.defineProperty(exports, "__esModule", { value: true });
14
- exports.Color = void 0;
15
- const chalk_1 = __importDefault(require("chalk"));
16
- class Color {
17
- static get bold() {
18
- return Color.chalk.bold;
19
- }
20
- static get purple() {
21
- return Color.chalk.hex('#7628c8');
22
- }
23
- static get darkPurple() {
24
- return Color.chalk.hex('#501b86');
25
- }
26
- static get yellow() {
27
- return Color.chalk.hex('#ffe600');
28
- }
29
- static get cyan() {
30
- return Color.chalk.hex('#00ffff');
31
- }
32
- static get white() {
33
- return Color.chalk.hex('#ffffff');
34
- }
35
- static get orange() {
36
- return Color.chalk.hex('#f18b0e');
37
- }
38
- static get green() {
39
- return Color.chalk.hex('#0ef12c');
40
- }
41
- static get darkGreen() {
42
- return Color.chalk.hex('#1cb70b');
43
- }
44
- static get red() {
45
- return Color.chalk.hex('#f10e0e');
46
- }
47
- static get info() {
48
- return this.cyan.bold;
49
- }
50
- static get log() {
51
- return this.green.bold;
52
- }
53
- static get debug() {
54
- return this.purple.bold;
55
- }
56
- static get error() {
57
- return this.red.bold;
58
- }
59
- static get warning() {
60
- return this.orange.bold;
61
- }
62
- static get GET() {
63
- return this.purple.bold;
64
- }
65
- static get HEAD() {
66
- return this.cyan.bold;
67
- }
68
- static get PUT() {
69
- return this.orange.bold;
70
- }
71
- static get PATCH() {
72
- return this.yellow.bold;
73
- }
74
- static get POST() {
75
- return this.green.bold;
76
- }
77
- static get DELETE() {
78
- return this.red.bold;
79
- }
80
- static get OPTIONS() {
81
- return this.cyan.bold;
82
- }
83
- static removeColors(string) {
84
- return Color.chalk.reset(string).replace(
85
- // eslint-disable-next-line no-control-regex
86
- /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, '');
87
- }
88
- static httpMethod(method) {
89
- return this[method];
90
- }
91
- }
92
- exports.Color = Color;
93
- Color.chalk = chalk_1.default;
@@ -1,9 +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
- export declare function getTimestamp(): string;
@@ -1,23 +0,0 @@
1
- "use strict";
2
- /**
3
- * @athenna/logger
4
- *
5
- * (c) João Lenon <lenon@athenna.io>
6
- *
7
- * For the full copyright and license information, please view the LICENSE
8
- * file that was distributed with this source code.
9
- */
10
- Object.defineProperty(exports, "__esModule", { value: true });
11
- exports.getTimestamp = void 0;
12
- function getTimestamp() {
13
- const localeStringOptions = {
14
- year: 'numeric',
15
- hour: 'numeric',
16
- minute: 'numeric',
17
- second: 'numeric',
18
- day: '2-digit',
19
- month: '2-digit',
20
- };
21
- return new Date(Date.now()).toLocaleString(undefined, localeStringOptions);
22
- }
23
- exports.getTimestamp = getTimestamp;