@athenna/logger 1.1.5 → 1.1.8

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 (39) hide show
  1. package/index.d.ts +1 -0
  2. package/index.js +1 -0
  3. package/package.json +7 -2
  4. package/src/Contracts/DefaultDriverConfigs.d.ts +4 -0
  5. package/src/Contracts/DefaultDriverConfigs.js +2 -0
  6. package/src/Contracts/DriverContract.d.ts +2 -0
  7. package/src/Drivers/ConsoleDriver.d.ts +4 -10
  8. package/src/Drivers/ConsoleDriver.js +6 -11
  9. package/src/Drivers/DebugDriver.d.ts +4 -10
  10. package/src/Drivers/DebugDriver.js +6 -11
  11. package/src/Drivers/DiscordDriver.d.ts +20 -0
  12. package/src/Drivers/DiscordDriver.js +34 -0
  13. package/src/Drivers/FileDriver.d.ts +4 -8
  14. package/src/Drivers/FileDriver.js +6 -11
  15. package/src/Drivers/NullDriver.d.ts +13 -0
  16. package/src/Drivers/NullDriver.js +16 -0
  17. package/src/Drivers/PinoDriver.d.ts +20 -0
  18. package/src/Drivers/PinoDriver.js +72 -0
  19. package/src/Drivers/SlackDriver.d.ts +19 -0
  20. package/src/Drivers/SlackDriver.js +31 -0
  21. package/src/Drivers/TelegramDriver.d.ts +21 -0
  22. package/src/Drivers/TelegramDriver.js +30 -0
  23. package/src/Exceptions/NotFoundFormatterException.js +1 -1
  24. package/src/Exceptions/OnlyPinoPrettyException.d.ts +12 -0
  25. package/src/Exceptions/OnlyPinoPrettyException.js +19 -0
  26. package/src/Facades/Log.d.ts +10 -0
  27. package/src/Facades/Log.js +13 -0
  28. package/src/Factories/DriverFactory.js +11 -1
  29. package/src/Factories/FormatterFactory.js +2 -0
  30. package/src/Formatters/CliFormatter.js +2 -0
  31. package/src/Formatters/MessageFormatter.d.ts +20 -0
  32. package/src/Formatters/MessageFormatter.js +43 -0
  33. package/src/Formatters/SimpleFormatter.d.ts +2 -2
  34. package/src/Formatters/SimpleFormatter.js +2 -0
  35. package/src/Logger.d.ts +91 -2
  36. package/src/Logger.js +89 -13
  37. package/src/Providers/LoggerProvider.js +1 -1
  38. package/src/Utils/groupConfigs.d.ts +9 -0
  39. package/src/Utils/groupConfigs.js +18 -0
package/index.d.ts CHANGED
@@ -7,6 +7,7 @@
7
7
  * file that was distributed with this source code.
8
8
  */
9
9
  export * from './src/Logger';
10
+ export * from './src/Facades/Log';
10
11
  export * from './src/Utils/Color';
11
12
  export * from './src/Contracts/DriverContract';
12
13
  export * from './src/Contracts/FormatterContract';
package/index.js CHANGED
@@ -19,6 +19,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
19
19
  };
20
20
  Object.defineProperty(exports, "__esModule", { value: true });
21
21
  __exportStar(require("./src/Logger"), exports);
22
+ __exportStar(require("./src/Facades/Log"), exports);
22
23
  __exportStar(require("./src/Utils/Color"), exports);
23
24
  __exportStar(require("./src/Contracts/DriverContract"), exports);
24
25
  __exportStar(require("./src/Contracts/FormatterContract"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@athenna/logger",
3
- "version": "1.1.5",
3
+ "version": "1.1.8",
4
4
  "description": "",
5
5
  "license": "MIT",
6
6
  "author": "João Lenon <lenon@athenna.io>",
@@ -122,6 +122,7 @@
122
122
  "dot-notation": "off",
123
123
  "camelcase": "off",
124
124
  "no-undef": "off",
125
+ "no-useless-escape": "off",
125
126
  "@typescript-eslint/no-var-requires": "off",
126
127
  "no-useless-constructor": "off",
127
128
  "@typescript-eslint/no-useless-constructor": "off",
@@ -153,10 +154,14 @@
153
154
  }
154
155
  },
155
156
  "dependencies": {
156
- "@athenna/ioc": "1.1.2",
157
+ "@athenna/ioc": "1.1.3",
157
158
  "@secjs/utils": "1.8.3",
159
+ "axios": "0.26.1",
158
160
  "chalk": "4.1.1",
161
+ "pino": "7.10.0",
162
+ "pino-pretty": "7.6.1",
159
163
  "reflect-metadata": "0.1.13",
164
+ "telegraf": "4.7.0",
160
165
  "tscpaths": "0.0.9"
161
166
  }
162
167
  }
@@ -0,0 +1,4 @@
1
+ export interface DefaultDriverConfigs {
2
+ formatter: string;
3
+ formatterConfig: any;
4
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -6,6 +6,8 @@
6
6
  * For the full copyright and license information, please view the LICENSE
7
7
  * file that was distributed with this source code.
8
8
  */
9
+ import { DefaultDriverConfigs } from './DefaultDriverConfigs';
9
10
  export interface DriverContract {
11
+ configs?: DefaultDriverConfigs | any;
10
12
  transport(message: string, options?: any): void | Promise<void>;
11
13
  }
@@ -6,20 +6,14 @@
6
6
  * For the full copyright and license information, please view the LICENSE
7
7
  * file that was distributed with this source code.
8
8
  */
9
- import { Color } from '../Utils/Color';
10
9
  import { DriverContract } from '../Contracts/DriverContract';
11
10
  export interface ConsoleDriverOpts {
12
- color: Color;
13
- level: string;
14
- context: string;
15
- formatter: string;
16
- streamType: string;
17
- formatterConfig: any;
11
+ streamType?: 'stdout' | 'stderr';
12
+ formatter?: any;
13
+ formatterConfig?: any;
18
14
  }
19
15
  export declare class ConsoleDriver implements DriverContract {
20
- private readonly _formatter;
21
- private readonly _streamType;
22
- private readonly _formatterConfig;
16
+ configs: Required<ConsoleDriverOpts>;
23
17
  constructor(channel: string, configs?: any);
24
18
  transport(message: string, options?: ConsoleDriverOpts): void;
25
19
  }
@@ -10,22 +10,17 @@
10
10
  Object.defineProperty(exports, "__esModule", { value: true });
11
11
  exports.ConsoleDriver = void 0;
12
12
  const utils_1 = require("@secjs/utils");
13
+ const groupConfigs_1 = require("../Utils/groupConfigs");
13
14
  const FormatterFactory_1 = require("../Factories/FormatterFactory");
14
15
  class ConsoleDriver {
15
16
  constructor(channel, configs = {}) {
16
17
  const channelConfig = utils_1.Config.get(`logging.channels.${channel}`);
17
- this._formatter = configs.formatter || channelConfig.formatter;
18
- this._streamType = configs.streamType || channelConfig.streamType;
19
- this._formatterConfig = Object.assign({}, channelConfig.formatterConfig, configs.formatterConfig);
18
+ this.configs = groupConfigs_1.groupConfigs(configs, channelConfig);
20
19
  }
21
- transport(message, options) {
22
- options = Object.assign({}, {
23
- formatter: this._formatter,
24
- streamType: this._streamType,
25
- }, options);
26
- const formatterOptions = Object.assign({}, this._formatterConfig, options.formatterConfig);
27
- message = FormatterFactory_1.FormatterFactory.fabricate(options.formatter).format(message, formatterOptions);
28
- process[options.streamType].write(`${message}\n`);
20
+ transport(message, options = {}) {
21
+ const configs = groupConfigs_1.groupConfigs(options, this.configs);
22
+ message = FormatterFactory_1.FormatterFactory.fabricate(configs.formatter).format(message, configs.formatterConfig);
23
+ process[configs.streamType].write(`${message}\n`);
29
24
  }
30
25
  }
31
26
  exports.ConsoleDriver = ConsoleDriver;
@@ -6,20 +6,14 @@
6
6
  * For the full copyright and license information, please view the LICENSE
7
7
  * file that was distributed with this source code.
8
8
  */
9
- import { Color } from '../Utils/Color';
10
9
  import { DriverContract } from '../Contracts/DriverContract';
11
10
  export interface DebugDriverOpts {
12
- color: Color;
13
- level: string;
14
- context: string;
15
- formatter: string;
16
- namespace: string;
17
- formatterConfig: any;
11
+ namespace?: string;
12
+ formatter?: any;
13
+ formatterConfig?: any;
18
14
  }
19
15
  export declare class DebugDriver implements DriverContract {
20
- private readonly _formatter;
21
- private readonly _namespace;
22
- private readonly _formatterConfig;
16
+ configs: Required<DebugDriverOpts>;
23
17
  constructor(channel: string, configs?: any);
24
18
  transport(message: string, options?: DebugDriverOpts): void;
25
19
  }
@@ -11,22 +11,17 @@ Object.defineProperty(exports, "__esModule", { value: true });
11
11
  exports.DebugDriver = void 0;
12
12
  const debug_1 = require("debug");
13
13
  const utils_1 = require("@secjs/utils");
14
+ const groupConfigs_1 = require("../Utils/groupConfigs");
14
15
  const FormatterFactory_1 = require("../Factories/FormatterFactory");
15
16
  class DebugDriver {
16
17
  constructor(channel, configs = {}) {
17
18
  const channelConfig = utils_1.Config.get(`logging.channels.${channel}`);
18
- this._formatter = configs.formatter || channelConfig.formatter;
19
- this._namespace = configs.namespace || channelConfig.namespace;
20
- this._formatterConfig = Object.assign({}, channelConfig.formatterConfig, configs.formatterConfig);
19
+ this.configs = groupConfigs_1.groupConfigs(configs, channelConfig);
21
20
  }
22
- transport(message, options) {
23
- options = Object.assign({}, {
24
- formatter: this._formatter,
25
- namespace: this._namespace,
26
- }, options);
27
- const formatterOptions = Object.assign({}, this._formatterConfig, options.formatterConfig);
28
- message = FormatterFactory_1.FormatterFactory.fabricate(options.formatter).format(message, formatterOptions);
29
- debug_1.debug(options.namespace)(message);
21
+ transport(message, options = {}) {
22
+ const configs = groupConfigs_1.groupConfigs(options, this.configs);
23
+ message = FormatterFactory_1.FormatterFactory.fabricate(configs.formatter).format(message, configs.formatterConfig);
24
+ debug_1.debug(configs.namespace)(message);
30
25
  }
31
26
  }
32
27
  exports.DebugDriver = DebugDriver;
@@ -0,0 +1,20 @@
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 { DriverContract } from '../Contracts/DriverContract';
10
+ export interface DiscordDriverOpts {
11
+ url?: string;
12
+ username?: string;
13
+ formatter?: any;
14
+ formatterConfig?: any;
15
+ }
16
+ export declare class DiscordDriver implements DriverContract {
17
+ configs: Required<DiscordDriverOpts>;
18
+ constructor(channel: string, configs?: any);
19
+ transport(message: string, options?: DiscordDriverOpts): Promise<void>;
20
+ }
@@ -0,0 +1,34 @@
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.DiscordDriver = void 0;
15
+ const axios_1 = __importDefault(require("axios"));
16
+ const utils_1 = require("@secjs/utils");
17
+ const Color_1 = require("../Utils/Color");
18
+ const groupConfigs_1 = require("../Utils/groupConfigs");
19
+ const FormatterFactory_1 = require("../Factories/FormatterFactory");
20
+ class DiscordDriver {
21
+ constructor(channel, configs = {}) {
22
+ const channelConfig = utils_1.Config.get(`logging.channels.${channel}`);
23
+ this.configs = groupConfigs_1.groupConfigs(configs, channelConfig);
24
+ }
25
+ async transport(message, options = {}) {
26
+ const configs = groupConfigs_1.groupConfigs(options, this.configs);
27
+ message = FormatterFactory_1.FormatterFactory.fabricate(configs.formatter).format(message, configs.formatterConfig);
28
+ await axios_1.default.post(configs.url, {
29
+ username: configs.username,
30
+ content: Color_1.Color.removeColors(message),
31
+ });
32
+ }
33
+ }
34
+ exports.DiscordDriver = DiscordDriver;
@@ -8,16 +8,12 @@
8
8
  */
9
9
  import { DriverContract } from '../Contracts/DriverContract';
10
10
  export interface FileDriverOpts {
11
- level: string;
12
- context: string;
13
- formatter: string;
14
- filePath: string;
15
- formatterConfig: any;
11
+ filePath?: string;
12
+ formatter?: any;
13
+ formatterConfig?: any;
16
14
  }
17
15
  export declare class FileDriver implements DriverContract {
18
- private readonly _filePath;
19
- private readonly _formatter;
20
- private readonly _formatterConfig;
16
+ configs: Required<FileDriverOpts>;
21
17
  constructor(channel: string, configs?: any);
22
18
  transport(message: string, options?: FileDriverOpts): Promise<void>;
23
19
  }
@@ -12,27 +12,22 @@ exports.FileDriver = void 0;
12
12
  const path_1 = require("path");
13
13
  const utils_1 = require("@secjs/utils");
14
14
  const Color_1 = require("../Utils/Color");
15
+ const groupConfigs_1 = require("../Utils/groupConfigs");
15
16
  const fs_1 = require("fs");
16
17
  const FormatterFactory_1 = require("../Factories/FormatterFactory");
17
18
  class FileDriver {
18
19
  constructor(channel, configs = {}) {
19
20
  const channelConfig = utils_1.Config.get(`logging.channels.${channel}`);
20
- this._filePath = configs.filePath || channelConfig.filePath;
21
- this._formatter = configs.formatter || channelConfig.formatter;
22
- this._formatterConfig = Object.assign({}, channelConfig.formatterConfig, configs.formatterConfig);
21
+ this.configs = groupConfigs_1.groupConfigs(configs, channelConfig);
23
22
  }
24
- async transport(message, options) {
25
- options = Object.assign({}, {
26
- filePath: this._filePath,
27
- formatter: this._formatter,
28
- }, options);
29
- const filePath = options.filePath;
23
+ async transport(message, options = {}) {
24
+ const configs = groupConfigs_1.groupConfigs(options, this.configs);
25
+ const filePath = configs.filePath;
30
26
  const { dir } = path_1.parse(filePath);
31
27
  if (!fs_1.existsSync(dir)) {
32
28
  fs_1.mkdirSync(dir, { recursive: true });
33
29
  }
34
- const formatterOptions = Object.assign({}, this._formatterConfig, options.formatterConfig);
35
- message = FormatterFactory_1.FormatterFactory.fabricate(options.formatter).format(message, formatterOptions);
30
+ message = FormatterFactory_1.FormatterFactory.fabricate(configs.formatter).format(message, configs.formatterConfig);
36
31
  return new Promise((resolve, reject) => {
37
32
  const stream = fs_1.createWriteStream(filePath, { flags: 'a' });
38
33
  stream.write(`${Color_1.Color.removeColors(message)}` + '\n');
@@ -0,0 +1,13 @@
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 { DriverContract } from '../Contracts/DriverContract';
10
+ export declare class NullDriver implements DriverContract {
11
+ constructor(_channel: string, _configs?: any);
12
+ transport(_message: string, _options?: any): void;
13
+ }
@@ -0,0 +1,16 @@
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.NullDriver = void 0;
12
+ class NullDriver {
13
+ constructor(_channel, _configs = {}) { }
14
+ transport(_message, _options) { }
15
+ }
16
+ exports.NullDriver = NullDriver;
@@ -0,0 +1,20 @@
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 pino from 'pino';
10
+ import pinoPretty from 'pino-pretty';
11
+ import { DriverContract } from '../Contracts/DriverContract';
12
+ export interface PinoDriverOpts extends pino.LoggerOptions {
13
+ formatter: 'pino-pretty';
14
+ formatterConfig: pinoPretty.PrettyOptions;
15
+ }
16
+ export declare class PinoDriver implements DriverContract {
17
+ configs: PinoDriverOpts;
18
+ constructor(channel: string, configs?: any);
19
+ transport(message: any, options?: Partial<PinoDriverOpts>): void;
20
+ }
@@ -0,0 +1,72 @@
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.PinoDriver = void 0;
15
+ const pino_1 = __importDefault(require("pino"));
16
+ const utils_1 = require("@secjs/utils");
17
+ const groupConfigs_1 = require("../Utils/groupConfigs");
18
+ const OnlyPinoPrettyException_1 = require("../Exceptions/OnlyPinoPrettyException");
19
+ class PinoDriver {
20
+ constructor(channel, configs = {}) {
21
+ const channelConfig = utils_1.Config.get(`logging.channels.${channel}`);
22
+ this.configs = groupConfigs_1.groupConfigs(configs, channelConfig);
23
+ }
24
+ transport(message, options = {}) {
25
+ const configs = groupConfigs_1.groupConfigs(options, this.configs);
26
+ configs.customLevels = {
27
+ info: 1,
28
+ warn: 2,
29
+ error: 3,
30
+ debug: 4,
31
+ success: 5,
32
+ };
33
+ configs.useOnlyCustomLevels = true;
34
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
35
+ // @ts-ignore
36
+ const pinoMethod = configs.formatterConfig.level.toLowerCase();
37
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
38
+ // @ts-ignore
39
+ delete configs.formatterConfig.level;
40
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
41
+ // @ts-ignore
42
+ delete configs.formatterConfig.chalk;
43
+ if (configs.formatter !== 'pino-pretty') {
44
+ throw new OnlyPinoPrettyException_1.OnlyPinoPrettyException();
45
+ }
46
+ const pinoConfigs = {};
47
+ Object.keys(configs).forEach(key => {
48
+ if (key === 'formatter') {
49
+ pinoConfigs.transport = {
50
+ target: 'pino-pretty',
51
+ options: configs.formatterConfig,
52
+ };
53
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
54
+ // @ts-ignore
55
+ configs.formatterConfig.customLevels =
56
+ 'info:1,warn:2,error:3,debug:4,success:5';
57
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
58
+ // @ts-ignore
59
+ configs.formatterConfig.customColors =
60
+ 'info:cyan,warn:yellow,error:red,debug:magenta,success:green';
61
+ return;
62
+ }
63
+ if (['driver', 'formatterConfig'].includes(key)) {
64
+ return;
65
+ }
66
+ pinoConfigs[key] = configs[key];
67
+ });
68
+ const logger = pino_1.default(pinoConfigs);
69
+ return logger[pinoMethod](message);
70
+ }
71
+ }
72
+ exports.PinoDriver = PinoDriver;
@@ -0,0 +1,19 @@
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 { DriverContract } from '../Contracts/DriverContract';
10
+ export interface SlackDriverOpts {
11
+ url?: string;
12
+ formatter?: any;
13
+ formatterConfig?: any;
14
+ }
15
+ export declare class SlackDriver implements DriverContract {
16
+ configs: Required<SlackDriverOpts>;
17
+ constructor(channel: string, configs?: any);
18
+ transport(message: string, options?: SlackDriverOpts): Promise<void>;
19
+ }
@@ -0,0 +1,31 @@
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.SlackDriver = void 0;
15
+ const axios_1 = __importDefault(require("axios"));
16
+ const utils_1 = require("@secjs/utils");
17
+ const Color_1 = require("../Utils/Color");
18
+ const groupConfigs_1 = require("../Utils/groupConfigs");
19
+ const FormatterFactory_1 = require("../Factories/FormatterFactory");
20
+ class SlackDriver {
21
+ constructor(channel, configs = {}) {
22
+ const channelConfig = utils_1.Config.get(`logging.channels.${channel}`);
23
+ this.configs = groupConfigs_1.groupConfigs(configs, channelConfig);
24
+ }
25
+ async transport(message, options = {}) {
26
+ const configs = groupConfigs_1.groupConfigs(options, this.configs);
27
+ message = FormatterFactory_1.FormatterFactory.fabricate(configs.formatter).format(message, configs.formatterConfig);
28
+ await axios_1.default.post(configs.url, { text: Color_1.Color.removeColors(message) });
29
+ }
30
+ }
31
+ exports.SlackDriver = SlackDriver;
@@ -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
+ import { DriverContract } from '../Contracts/DriverContract';
10
+ export interface TelegramDriverOpts {
11
+ token?: string;
12
+ chatId?: string | number;
13
+ parseMode?: 'HTML' | 'Markdown' | 'MarkdownV2';
14
+ formatter?: any;
15
+ formatterConfig?: any;
16
+ }
17
+ export declare class TelegramDriver implements DriverContract {
18
+ configs: Required<TelegramDriverOpts>;
19
+ constructor(channel: string, configs?: any);
20
+ transport(message: string, options?: TelegramDriverOpts): Promise<void>;
21
+ }
@@ -0,0 +1,30 @@
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.TelegramDriver = void 0;
12
+ const telegraf_1 = require("telegraf");
13
+ const utils_1 = require("@secjs/utils");
14
+ const Color_1 = require("../Utils/Color");
15
+ const groupConfigs_1 = require("../Utils/groupConfigs");
16
+ const FormatterFactory_1 = require("../Factories/FormatterFactory");
17
+ class TelegramDriver {
18
+ constructor(channel, configs = {}) {
19
+ const channelConfig = utils_1.Config.get(`logging.channels.${channel}`);
20
+ this.configs = groupConfigs_1.groupConfigs(configs, channelConfig);
21
+ }
22
+ async transport(message, options = {}) {
23
+ const configs = groupConfigs_1.groupConfigs(options, this.configs);
24
+ message = FormatterFactory_1.FormatterFactory.fabricate(configs.formatter).format(message, configs.formatterConfig);
25
+ await new telegraf_1.Telegraf(configs.token).telegram.sendMessage(configs.chatId, Color_1.Color.removeColors(message), {
26
+ parse_mode: configs.parseMode,
27
+ });
28
+ }
29
+ }
30
+ exports.TelegramDriver = TelegramDriver;
@@ -12,7 +12,7 @@ exports.NotFoundFormatterException = void 0;
12
12
  const utils_1 = require("@secjs/utils");
13
13
  class NotFoundFormatterException extends utils_1.Exception {
14
14
  constructor(formatterName) {
15
- const content = `The driver ${formatterName} has not been found`;
15
+ const content = `The formatter ${formatterName} has not been found`;
16
16
  super(content, 500, 'NOT_FOUND_ERROR', `Look into your config/logger file if this formatter is implemented by logger. Or create this formatter implementation using Logger.buildFormatter method`);
17
17
  }
18
18
  }
@@ -0,0 +1,12 @@
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 { Exception } from '@secjs/utils';
10
+ export declare class OnlyPinoPrettyException extends Exception {
11
+ constructor();
12
+ }
@@ -0,0 +1,19 @@
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.OnlyPinoPrettyException = void 0;
12
+ const utils_1 = require("@secjs/utils");
13
+ class OnlyPinoPrettyException extends utils_1.Exception {
14
+ constructor() {
15
+ const content = `The driver "pino" can only be used with "pino-pretty" formatter.`;
16
+ super(content, 500, 'ONLY_PINO-PRETTY_EXCEPTION', `Look into your config/logger file the formatter that you are trying to use with your channel configuration.`);
17
+ }
18
+ }
19
+ exports.OnlyPinoPrettyException = OnlyPinoPrettyException;
@@ -0,0 +1,10 @@
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 { Logger } from '../Logger';
10
+ export declare const Log: Logger;
@@ -0,0 +1,13 @@
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.Log = void 0;
12
+ const ioc_1 = require("@athenna/ioc");
13
+ exports.Log = ioc_1.Facade.createFor('Athenna/Core/Logger');
@@ -10,9 +10,14 @@
10
10
  Object.defineProperty(exports, "__esModule", { value: true });
11
11
  exports.DriverFactory = void 0;
12
12
  const utils_1 = require("@secjs/utils");
13
+ const PinoDriver_1 = require("../Drivers/PinoDriver");
13
14
  const FileDriver_1 = require("../Drivers/FileDriver");
15
+ const NullDriver_1 = require("../Drivers/NullDriver");
14
16
  const DebugDriver_1 = require("../Drivers/DebugDriver");
17
+ const SlackDriver_1 = require("../Drivers/SlackDriver");
15
18
  const ConsoleDriver_1 = require("../Drivers/ConsoleDriver");
19
+ const DiscordDriver_1 = require("../Drivers/DiscordDriver");
20
+ const TelegramDriver_1 = require("../Drivers/TelegramDriver");
16
21
  const NotFoundDriverException_1 = require("../Exceptions/NotFoundDriverException");
17
22
  const DriverAlreadyExistException_1 = require("../Exceptions/DriverAlreadyExistException");
18
23
  const ChannelNotConfiguredException_1 = require("../Exceptions/ChannelNotConfiguredException");
@@ -55,5 +60,10 @@ class DriverFactory {
55
60
  exports.DriverFactory = DriverFactory;
56
61
  DriverFactory.drivers = new Map()
57
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 })
58
66
  .set('debug', { Driver: DebugDriver_1.DebugDriver })
59
- .set('console', { Driver: ConsoleDriver_1.ConsoleDriver });
67
+ .set('console', { Driver: ConsoleDriver_1.ConsoleDriver })
68
+ .set('discord', { Driver: DiscordDriver_1.DiscordDriver })
69
+ .set('telegram', { Driver: TelegramDriver_1.TelegramDriver });
@@ -13,6 +13,7 @@ const CliFormatter_1 = require("../Formatters/CliFormatter");
13
13
  const JsonFormatter_1 = require("../Formatters/JsonFormatter");
14
14
  const NestFormatter_1 = require("../Formatters/NestFormatter");
15
15
  const SimpleFormatter_1 = require("../Formatters/SimpleFormatter");
16
+ const MessageFormatter_1 = require("../Formatters/MessageFormatter");
16
17
  const RequestFormatter_1 = require("../Formatters/RequestFormatter");
17
18
  const NotFoundFormatterException_1 = require("../Exceptions/NotFoundFormatterException");
18
19
  const FormatterAlreadyExistException_1 = require("../Exceptions/FormatterAlreadyExistException");
@@ -44,4 +45,5 @@ FormatterFactory.formatters = new Map()
44
45
  .set('nest', { Formatter: NestFormatter_1.NestFormatter })
45
46
  .set('json', { Formatter: JsonFormatter_1.JsonFormatter })
46
47
  .set('simple', { Formatter: SimpleFormatter_1.SimpleFormatter })
48
+ .set('message', { Formatter: MessageFormatter_1.MessageFormatter })
47
49
  .set('request', { Formatter: RequestFormatter_1.RequestFormatter });
@@ -19,6 +19,8 @@ class CliFormatter {
19
19
  error: Color_1.Color.error,
20
20
  success: Color_1.Color.log,
21
21
  };
22
+ if (!levelColors[level.toLowerCase()])
23
+ return `[ ${level.toLowerCase()} ]`;
22
24
  return levelColors[level.toLowerCase()](`[ ${level.toLowerCase()} ]`);
23
25
  }
24
26
  format(message, options) {
@@ -0,0 +1,20 @@
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 { LevelTypes } from '../Contracts/LevelTypes';
10
+ import { FormatterContract } from '../Contracts/FormatterContract';
11
+ export interface MessageFormatterOpts {
12
+ level: LevelTypes;
13
+ customEmoji: string;
14
+ }
15
+ export declare class MessageFormatter implements FormatterContract {
16
+ private static lastTimestamp?;
17
+ private static getTimestampDiff;
18
+ private static getEmojiByLevel;
19
+ format(message: string, options: MessageFormatterOpts): string;
20
+ }
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ /* eslint-disable prettier/prettier */
3
+ /**
4
+ * @athenna/logger
5
+ *
6
+ * (c) João Lenon <lenon@athenna.io>
7
+ *
8
+ * For the full copyright and license information, please view the LICENSE
9
+ * file that was distributed with this source code.
10
+ */
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.MessageFormatter = void 0;
13
+ const Color_1 = require("../Utils/Color");
14
+ class MessageFormatter {
15
+ static getTimestampDiff() {
16
+ let result = '';
17
+ if (this.lastTimestamp) {
18
+ result = Color_1.Color.yellow(` +${Date.now() - this.lastTimestamp}ms`);
19
+ }
20
+ this.lastTimestamp = Date.now();
21
+ return result;
22
+ }
23
+ static getEmojiByLevel(level, customEmoji) {
24
+ if (customEmoji)
25
+ return customEmoji;
26
+ const levelEmojis = {
27
+ info: '\u{2139}',
28
+ debug: '\u{1F50E}',
29
+ warn: '\u{26A0}',
30
+ error: '\u{274C}',
31
+ success: '\u{2705}',
32
+ };
33
+ if (!levelEmojis[level.toLowerCase()])
34
+ return '';
35
+ return levelEmojis[level.toLowerCase()];
36
+ }
37
+ format(message, options) {
38
+ const timestampDiff = MessageFormatter.getTimestampDiff();
39
+ const level = MessageFormatter.getEmojiByLevel(options.level, options.customEmoji);
40
+ return `${level} ${message}${timestampDiff}`;
41
+ }
42
+ }
43
+ exports.MessageFormatter = MessageFormatter;
@@ -9,7 +9,7 @@
9
9
  import { Chalk } from 'chalk';
10
10
  import { LevelTypes } from '../Contracts/LevelTypes';
11
11
  import { FormatterContract } from '../Contracts/FormatterContract';
12
- export interface LogFormatterOptions {
12
+ export interface SimpleFormatterOpts {
13
13
  chalk: Chalk;
14
14
  level: LevelTypes;
15
15
  }
@@ -17,5 +17,5 @@ export declare class SimpleFormatter implements FormatterContract {
17
17
  private static lastTimestamp?;
18
18
  private static getTimestampDiff;
19
19
  private static paintByLevel;
20
- format(message: string, options: LogFormatterOptions): string;
20
+ format(message: string, options: SimpleFormatterOpts): string;
21
21
  }
@@ -28,6 +28,8 @@ class SimpleFormatter {
28
28
  error: Color_1.Color.error,
29
29
  success: Color_1.Color.log,
30
30
  };
31
+ if (!levelColors[level.toLowerCase()])
32
+ return `[${level.toUpperCase()}]`;
31
33
  return levelColors[level.toLowerCase()](`[${level.toUpperCase()}]`);
32
34
  }
33
35
  format(message, options) {
package/src/Logger.d.ts CHANGED
@@ -1,21 +1,110 @@
1
1
  import { DriverContract } from './Contracts/DriverContract';
2
2
  import { FormatterContract } from './Contracts/FormatterContract';
3
3
  export declare class Logger {
4
+ /**
5
+ * Runtime configurations to be used inside the Drivers and Formatters.
6
+ * @private
7
+ */
4
8
  private runtimeConfig;
5
- private channelName;
9
+ /**
10
+ * The driver responsible for transporting the logs.
11
+ * @private
12
+ */
6
13
  private driver;
7
- constructor(runtimeConfig?: any);
14
+ /**
15
+ * The log channel selected with driver and formatter configurations.
16
+ * @private
17
+ */
18
+ private channelName;
19
+ /**
20
+ * Creates a new instance of Logger.
21
+ *
22
+ * @return {Logger}
23
+ */
24
+ constructor();
25
+ /**
26
+ * Return all drivers available.
27
+ */
8
28
  static get drivers(): string[];
29
+ /**
30
+ * Return all formatters available.
31
+ */
9
32
  static get formatters(): string[];
33
+ /**
34
+ * Builds a new driver to use within Logger class.
35
+ *
36
+ * @param name
37
+ * @param driver
38
+ */
10
39
  static buildDriver(name: string, driver: new (channel: string, configs?: any) => DriverContract): void;
40
+ /**
41
+ * Builds a new formatter to use within Logger class.
42
+ *
43
+ * @param name
44
+ * @param formatter
45
+ */
11
46
  static buildFormatter(name: string, formatter: new () => FormatterContract): void;
47
+ /**
48
+ * Applies the log engine to execute chalk methods of string.
49
+ *
50
+ * @param content
51
+ * @private
52
+ */
12
53
  private static applyLogEngine;
54
+ /**
55
+ * Change the log channel.
56
+ *
57
+ * @param channel
58
+ * @param runtimeConfig
59
+ */
13
60
  channel(channel: string, runtimeConfig?: any): Logger;
61
+ /**
62
+ * Creates a log of type log in channel.
63
+ * @param message
64
+ * @param options
65
+ */
14
66
  log(message: any, options?: {}): void | Promise<void>;
67
+ /**
68
+ * Creates a log of type info in channel.
69
+ *
70
+ * @param message
71
+ * @param options
72
+ */
15
73
  info(message: any, options?: {}): void | Promise<void>;
74
+ /**
75
+ * Creates a log of type warn in channel.
76
+ *
77
+ * @param message
78
+ * @param options
79
+ */
16
80
  warn(message: any, options?: {}): void | Promise<void>;
81
+ /**
82
+ * Creates a log of type error in channel.
83
+ *
84
+ * @param message
85
+ * @param options
86
+ */
17
87
  error(message: any, options?: {}): void | Promise<void>;
88
+ /**
89
+ * Creates a log of type debug in channel.
90
+ *
91
+ * @param message
92
+ * @param options
93
+ */
18
94
  debug(message: any, options?: {}): void | Promise<void>;
95
+ /**
96
+ * Creates a log of type success in channel.
97
+ *
98
+ * @param message
99
+ * @param options
100
+ */
19
101
  success(message: any, options?: {}): void | Promise<void>;
102
+ /**
103
+ * Create options concatenating client options with default options.
104
+ *
105
+ * @param options
106
+ * @param defaultValues
107
+ * @private
108
+ */
20
109
  private createOptions;
21
110
  }
package/src/Logger.js CHANGED
@@ -10,23 +10,60 @@ const Color_1 = require("./Utils/Color");
10
10
  const DriverFactory_1 = require("./Factories/DriverFactory");
11
11
  const FormatterFactory_1 = require("./Factories/FormatterFactory");
12
12
  class Logger {
13
- constructor(runtimeConfig = {}) {
14
- this.runtimeConfig = runtimeConfig;
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
+ */
15
28
  this.channelName = 'default';
16
29
  this.driver = DriverFactory_1.DriverFactory.fabricate(this.channelName, this.runtimeConfig);
17
30
  }
31
+ /**
32
+ * Return all drivers available.
33
+ */
18
34
  static get drivers() {
19
35
  return DriverFactory_1.DriverFactory.availableDrivers();
20
36
  }
37
+ /**
38
+ * Return all formatters available.
39
+ */
21
40
  static get formatters() {
22
41
  return FormatterFactory_1.FormatterFactory.availableFormatters();
23
42
  }
43
+ /**
44
+ * Builds a new driver to use within Logger class.
45
+ *
46
+ * @param name
47
+ * @param driver
48
+ */
24
49
  static buildDriver(name, driver) {
25
50
  DriverFactory_1.DriverFactory.createDriver(name, driver);
26
51
  }
52
+ /**
53
+ * Builds a new formatter to use within Logger class.
54
+ *
55
+ * @param name
56
+ * @param formatter
57
+ */
27
58
  static buildFormatter(name, formatter) {
28
59
  FormatterFactory_1.FormatterFactory.createFormatter(name, formatter);
29
60
  }
61
+ /**
62
+ * Applies the log engine to execute chalk methods of string.
63
+ *
64
+ * @param content
65
+ * @private
66
+ */
30
67
  static applyLogEngine(content) {
31
68
  if (utils_1.Is.String(content)) {
32
69
  const matches = content.match(/\({(.*?)} (.*?)\)/);
@@ -49,22 +86,37 @@ class Logger {
49
86
  }
50
87
  return content;
51
88
  }
89
+ /**
90
+ * Change the log channel.
91
+ *
92
+ * @param channel
93
+ * @param runtimeConfig
94
+ */
52
95
  channel(channel, runtimeConfig) {
53
96
  if (runtimeConfig)
54
97
  this.runtimeConfig = runtimeConfig;
55
98
  this.driver = DriverFactory_1.DriverFactory.fabricate(channel, this.runtimeConfig);
99
+ this.channelName = channel;
56
100
  return this;
57
101
  }
102
+ /**
103
+ * Creates a log of type log in channel.
104
+ * @param message
105
+ * @param options
106
+ */
58
107
  log(message, options = {}) {
59
- options = this.createOptions(options, {
60
- streamType: 'stdout',
61
- });
108
+ options = this.createOptions(options, {});
62
109
  message = Logger.applyLogEngine(message);
63
110
  return this.driver.transport(message, options);
64
111
  }
112
+ /**
113
+ * Creates a log of type info in channel.
114
+ *
115
+ * @param message
116
+ * @param options
117
+ */
65
118
  info(message, options = {}) {
66
119
  options = this.createOptions(options, {
67
- streamType: 'stdout',
68
120
  formatterConfig: {
69
121
  level: 'INFO',
70
122
  chalk: Color_1.Color.cyan,
@@ -73,9 +125,14 @@ class Logger {
73
125
  message = Logger.applyLogEngine(message);
74
126
  return this.driver.transport(message, options);
75
127
  }
128
+ /**
129
+ * Creates a log of type warn in channel.
130
+ *
131
+ * @param message
132
+ * @param options
133
+ */
76
134
  warn(message, options = {}) {
77
135
  options = this.createOptions(options, {
78
- streamType: 'stdout',
79
136
  formatterConfig: {
80
137
  level: 'WARN',
81
138
  chalk: Color_1.Color.orange,
@@ -84,9 +141,14 @@ class Logger {
84
141
  message = Logger.applyLogEngine(message);
85
142
  return this.driver.transport(message, options);
86
143
  }
144
+ /**
145
+ * Creates a log of type error in channel.
146
+ *
147
+ * @param message
148
+ * @param options
149
+ */
87
150
  error(message, options = {}) {
88
151
  options = this.createOptions(options, {
89
- streamType: 'stdout',
90
152
  formatterConfig: {
91
153
  level: 'ERROR',
92
154
  chalk: Color_1.Color.red,
@@ -95,9 +157,14 @@ class Logger {
95
157
  message = Logger.applyLogEngine(message);
96
158
  return this.driver.transport(message, options);
97
159
  }
160
+ /**
161
+ * Creates a log of type debug in channel.
162
+ *
163
+ * @param message
164
+ * @param options
165
+ */
98
166
  debug(message, options = {}) {
99
167
  options = this.createOptions(options, {
100
- streamType: 'stdout',
101
168
  formatterConfig: {
102
169
  level: 'DEBUG',
103
170
  chalk: Color_1.Color.purple,
@@ -106,9 +173,14 @@ class Logger {
106
173
  message = Logger.applyLogEngine(message);
107
174
  return this.driver.transport(message, options);
108
175
  }
176
+ /**
177
+ * Creates a log of type success in channel.
178
+ *
179
+ * @param message
180
+ * @param options
181
+ */
109
182
  success(message, options = {}) {
110
183
  options = this.createOptions(options, {
111
- streamType: 'stdout',
112
184
  formatterConfig: {
113
185
  level: 'SUCCESS',
114
186
  chalk: Color_1.Color.green,
@@ -117,6 +189,13 @@ class Logger {
117
189
  message = Logger.applyLogEngine(message);
118
190
  return this.driver.transport(message, options);
119
191
  }
192
+ /**
193
+ * Create options concatenating client options with default options.
194
+ *
195
+ * @param options
196
+ * @param defaultValues
197
+ * @private
198
+ */
120
199
  createOptions(options, defaultValues) {
121
200
  let formatterConfig = Object.assign({}, {
122
201
  ...defaultValues.formatterConfig,
@@ -127,9 +206,6 @@ class Logger {
127
206
  ...formatterConfig,
128
207
  };
129
208
  }
130
- options = Object.assign({}, {
131
- streamType: 'stdout',
132
- }, options);
133
209
  options = {
134
210
  ...options,
135
211
  formatterConfig,
@@ -18,7 +18,7 @@ class LoggerProvider extends ioc_1.ServiceProvider {
18
18
  * @return void
19
19
  */
20
20
  register() {
21
- this.container.instance('Athenna/Core/Logger', new Logger_1.Logger());
21
+ this.container.bind('Athenna/Core/Logger', Logger_1.Logger);
22
22
  }
23
23
  }
24
24
  exports.LoggerProvider = LoggerProvider;
@@ -0,0 +1,9 @@
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 groupConfigs<T = any>(main: any, fallback: any): Required<T>;
@@ -0,0 +1,18 @@
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.groupConfigs = void 0;
12
+ function groupConfigs(main, fallback) {
13
+ const formatter = main.formatter || fallback.formatter;
14
+ const formatterConfig = Object.assign({}, fallback.formatterConfig, main.formatterConfig);
15
+ const driverConfig = Object.assign({}, fallback, main);
16
+ return { ...driverConfig, formatter, formatterConfig };
17
+ }
18
+ exports.groupConfigs = groupConfigs;