@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/LICENSE.md CHANGED
@@ -2,20 +2,8 @@ MIT License
2
2
 
3
3
  Copyright (c) 2022 The Athenna Framework
4
4
 
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
11
6
 
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
14
8
 
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -1,18 +1,18 @@
1
- # Logger 🔍
1
+ # Template 📔
2
2
 
3
- > The Athenna logging solution. Log in stdout, files and buckets
3
+ > Template for new projects
4
4
 
5
5
  [![GitHub followers](https://img.shields.io/github/followers/athennaio.svg?style=social&label=Follow&maxAge=2592000)](https://github.com/athennaio?tab=followers)
6
- [![GitHub stars](https://img.shields.io/github/stars/athennaio/logger.svg?style=social&label=Star&maxAge=2592000)](https://github.com/athennaio/logger/stargazers/)
6
+ [![GitHub stars](https://img.shields.io/github/stars/AthennaIO/Logger.svg?style=social&label=Star&maxAge=2592000)](https://github.com/AthennaIO/Logger/stargazers/)
7
7
 
8
8
  <p>
9
9
  <a href="https://www.buymeacoffee.com/athenna" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: 41px !important;width: 174px !important;box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;-webkit-box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;" ></a>
10
10
  </p>
11
11
 
12
12
  <p>
13
- <img alt="GitHub language count" src="https://img.shields.io/github/languages/count/athennaio/logger?style=for-the-badge&logo=appveyor">
13
+ <img alt="GitHub language count" src="https://img.shields.io/github/languages/count/AthennaIO/Logger?style=for-the-badge&logo=appveyor">
14
14
 
15
- <img alt="Repository size" src="https://img.shields.io/github/repo-size/athennaio/logger?style=for-the-badge&logo=appveyor">
15
+ <img alt="Repository size" src="https://img.shields.io/github/repo-size/AthennaIO/Logger?style=for-the-badge&logo=appveyor">
16
16
 
17
17
  <img alt="License" src="https://img.shields.io/badge/license-MIT-brightgreen?style=for-the-badge&logo=appveyor">
18
18
 
@@ -27,7 +27,7 @@
27
27
 
28
28
  ## Contributing
29
29
 
30
- > If you want to contribute to this project, first read the [CONTRIBUTING.MD](https://github.com/athennaio/logger/blob/develop/CONTRIBUTING.md) file. It will be a pleasure to receive your help.
30
+ > If you want to contribute to this project, first read the [CONTRIBUTING.MD](https://github.com/AthennaIO/Logger/blob/develop/CONTRIBUTING.md) file. It will be a pleasure to receive your help.
31
31
 
32
32
  ---
33
33
 
package/package.json CHANGED
@@ -1,49 +1,92 @@
1
1
  {
2
2
  "name": "@athenna/logger",
3
- "version": "1.1.7",
4
- "description": "",
3
+ "version": "1.2.0",
4
+ "description": "Template for new projects.",
5
5
  "license": "MIT",
6
6
  "author": "João Lenon <lenon@athenna.io>",
7
- "repository": "https://github.com/AthennaIO/Logger.git",
8
7
  "bugs": "https://github.com/AthennaIO/Logger/issues",
8
+ "repository": "https://github.com/AthennaIO/Logger.git",
9
9
  "homepage": "https://github.com/AthennaIO/Logger#readme",
10
+ "keywords": [],
10
11
  "scripts": {
11
- "build": "tsc --project tsconfig.json && tscpaths -p tsconfig.json -s . -o .",
12
- "test": "npm run lint:fix && cross-env NODE_TS=true cross-env NODE_ENV=testing jest --verbose",
13
- "test:debug": "npm run lint:fix && cross-env NODE_TS=true cross-env NODE_ENV=testing cross-env DEBUG=api:* jest --verbose",
14
- "lint:fix": "eslint \"{src,tests}/**/*.ts\" --fix"
12
+ "lint:fix": "eslint \"{src,tests}/**/*.js\" --fix",
13
+ "test": "npm run --silent lint:fix && node ./tests/index.js",
14
+ "test:debug": "cross-env DEBUG=api:* && npm run --silent test",
15
+ "test:coverage": "c8 npm run --silent test",
16
+ "gen:types": "tsc src/*.js --declaration --allowJs --emitDeclarationOnly --out src/index.js"
15
17
  },
16
- "keywords": [
17
- "nodejs",
18
- "athenna",
19
- "typescript"
18
+ "files": [
19
+ "src/*.js",
20
+ "src/*.d.ts",
21
+ "src/**/*.js",
22
+ "src/**/*.d.ts"
20
23
  ],
24
+ "type": "module",
25
+ "main": "./src/index.js",
26
+ "types": "./src/index.d.ts",
27
+ "imports": {
28
+ "#src/*": "./src/*.js",
29
+ "#tests/*": "./tests/*.js"
30
+ },
31
+ "exports": {
32
+ ".": "./src/index.js",
33
+ "./facades/Log": "./src/Facades/Log.js",
34
+ "./providers/LoggerProvider": "./src/Providers/LoggerProvider.js"
35
+ },
36
+ "dependencies": {
37
+ "@athenna/ioc": "1.1.7",
38
+ "@secjs/utils": "1.9.3",
39
+ "awilix": "7.0.3",
40
+ "axios": "0.26.1",
41
+ "chalk": "5.0.1",
42
+ "debug": "4.3.4",
43
+ "pino": "7.10.0",
44
+ "pino-pretty": "7.6.1",
45
+ "telegraf": "4.7.0"
46
+ },
21
47
  "devDependencies": {
22
- "@types/jest": "27.0.1",
23
- "@types/node": "14.17.0",
24
- "@typescript-eslint/eslint-plugin": "4.31.0",
25
- "@typescript-eslint/parser": "4.31.0",
48
+ "@japa/assert": "1.3.4",
49
+ "@japa/run-failed-tests": "1.0.7",
50
+ "@japa/runner": "2.0.7",
51
+ "@japa/spec-reporter": "1.1.12",
52
+ "c8": "7.11.2",
26
53
  "commitizen": "4.2.4",
27
54
  "cross-env": "7.0.3",
28
55
  "cz-conventional-changelog": "3.3.0",
29
- "eslint": "7.32.0",
30
- "eslint-config-prettier": "8.3.0",
31
- "eslint-config-standard": "16.0.3",
32
- "eslint-import-resolver-typescript": "2.4.0",
33
- "eslint-plugin-import": "2.24.2",
34
- "eslint-plugin-node": "11.1.0",
56
+ "eslint": "8.14.0",
57
+ "eslint-config-prettier": "8.5.0",
58
+ "eslint-config-standard": "17.0.0",
59
+ "eslint-plugin-import": "2.26.0",
60
+ "eslint-plugin-n": "15.2.0",
35
61
  "eslint-plugin-prettier": "4.0.0",
36
- "eslint-plugin-promise": "5.1.0",
37
- "eslint-plugin-standard": "5.0.0",
62
+ "eslint-plugin-promise": "6.0.0",
38
63
  "husky": "3.0.9",
39
- "jest": "27.1.0",
40
- "lint-staged": "9.4.3",
41
- "prettier": "2.0.5",
42
- "ts-jest": "27.0.5",
43
- "ts-loader": "9.2.3",
44
- "ts-node": "10.0.0",
45
- "tsconfig-paths": "3.9.0",
46
- "typescript": "4.3.5"
64
+ "lint-staged": "12.4.1",
65
+ "minimist": "1.2.6",
66
+ "prettier": "2.6.2",
67
+ "rimraf": "3.0.2",
68
+ "typescript": "4.6.4"
69
+ },
70
+ "c8": {
71
+ "all": true,
72
+ "include": [
73
+ "src/**/*.js"
74
+ ],
75
+ "reporter": [
76
+ "text-summary",
77
+ "html"
78
+ ],
79
+ "report-dir": "./tests/Coverage",
80
+ "check-coverage": true,
81
+ "statements": "80",
82
+ "branches": "80",
83
+ "functions": "50",
84
+ "lines": "80"
85
+ },
86
+ "husky": {
87
+ "hooks": {
88
+ "prepare-commit-msg": "lint-staged && exec < /dev/tty && git cz --hook || true"
89
+ }
47
90
  },
48
91
  "lint-staged": {
49
92
  "*.js": [
@@ -55,30 +98,11 @@
55
98
  "git add"
56
99
  ]
57
100
  },
58
- "husky": {
59
- "hooks": {
60
- "pre-commit": "lint-staged",
61
- "prepare-commit-msg": "exec < /dev/tty && git cz --hook || true"
101
+ "config": {
102
+ "commitizen": {
103
+ "path": "./node_modules/cz-conventional-changelog"
62
104
  }
63
105
  },
64
- "jest": {
65
- "modulePaths": [
66
- "<rootDir>"
67
- ],
68
- "moduleFileExtensions": [
69
- "js",
70
- "json",
71
- "ts"
72
- ],
73
- "rootDir": ".",
74
- "testRegex": "Test.ts$",
75
- "transform": {
76
- "^.+\\.(t|j)s$": "ts-jest"
77
- },
78
- "coverageDirectory": "./coverage",
79
- "preset": "ts-jest",
80
- "testEnvironment": "node"
81
- },
82
106
  "prettier": {
83
107
  "singleQuote": true,
84
108
  "trailingComma": "all",
@@ -97,66 +121,29 @@
97
121
  },
98
122
  "eslintConfig": {
99
123
  "env": {
100
- "es6": true,
124
+ "es2021": true,
101
125
  "node": true
102
126
  },
127
+ "plugins": [
128
+ "prettier"
129
+ ],
103
130
  "extends": [
104
131
  "standard",
105
- "plugin:@typescript-eslint/recommended",
132
+ "eslint:recommended",
106
133
  "plugin:prettier/recommended"
107
134
  ],
108
- "globals": {
109
- "Atomics": "readonly",
110
- "SharedArrayBuffer": "readonly"
111
- },
112
- "parser": "@typescript-eslint/parser",
135
+ "ignorePatterns": [
136
+ "**/*.d.ts"
137
+ ],
113
138
  "parserOptions": {
114
- "ecmaVersion": 2018,
139
+ "ecmaVersion": "latest",
115
140
  "sourceType": "module"
116
141
  },
117
- "plugins": [
118
- "@typescript-eslint",
119
- "prettier"
120
- ],
121
142
  "rules": {
122
- "dot-notation": "off",
123
143
  "camelcase": "off",
124
- "no-undef": "off",
125
- "@typescript-eslint/no-var-requires": "off",
126
- "no-useless-constructor": "off",
127
- "@typescript-eslint/no-useless-constructor": "off",
128
- "@typescript-eslint/camelcase": "off",
129
- "@typescript-eslint/explicit-module-boundary-types": "off",
130
- "@typescript-eslint/interface-name-prefix": "off",
131
- "@typescript-eslint/no-explicit-any": "off",
144
+ "dot-notation": "off",
132
145
  "prettier/prettier": "error",
133
- "@typescript-eslint/no-empty-function": "off",
134
- "@typescript-eslint/no-unused-vars": [
135
- "error",
136
- {
137
- "argsIgnorePattern": "_"
138
- }
139
- ]
146
+ "no-useless-constructor": "off"
140
147
  }
141
- },
142
- "files": [
143
- "src/*.js",
144
- "src/*.d.ts",
145
- "src/**/*.js",
146
- "src/**/*.d.ts",
147
- "*.js",
148
- "*.d.ts"
149
- ],
150
- "config": {
151
- "commitizen": {
152
- "path": "./node_modules/cz-conventional-changelog"
153
- }
154
- },
155
- "dependencies": {
156
- "@athenna/ioc": "1.1.3",
157
- "@secjs/utils": "1.8.3",
158
- "chalk": "4.1.1",
159
- "reflect-metadata": "0.1.13",
160
- "tscpaths": "0.0.9"
161
148
  }
162
149
  }
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  /**
3
2
  * @athenna/logger
4
3
  *
@@ -7,25 +6,47 @@
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
- Object.defineProperty(exports, "__esModule", { value: true });
11
- exports.ConsoleDriver = void 0;
12
- const utils_1 = require("@secjs/utils");
13
- const FormatterFactory_1 = require("../Factories/FormatterFactory");
14
- class ConsoleDriver {
15
- constructor(channel, configs = {}) {
16
- 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);
20
- }
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`);
29
- }
9
+
10
+ import { Config } from '@secjs/utils'
11
+
12
+ import { FactoryHelper, FormatterFactory } from '#src/index'
13
+
14
+ export class ConsoleDriver {
15
+ /**
16
+ * Holds the configuration set of ConsoleDriver.
17
+ *
18
+ * @type {{ streamType?: 'stdout' | 'stderr', formatter?: any, formatterConfig?: any }}
19
+ */
20
+ configs
21
+
22
+ /**
23
+ * Creates a new instance of ConsoleDriver.
24
+ *
25
+ * @param {string} channel
26
+ * @param {any} [configs]
27
+ * @return {ConsoleDriver}
28
+ */
29
+ constructor(channel, configs) {
30
+ const channelConfig = Config.get(`logging.channels.${channel}`)
31
+
32
+ this.configs = FactoryHelper.groupConfigs(configs, channelConfig)
33
+ }
34
+
35
+ /**
36
+ * Transport the log.
37
+ *
38
+ * @param {string} message
39
+ * @param {{ streamType?: 'stdout' | 'stderr', formatter?: any, formatterConfig?: any }} [options]
40
+ * @return {void}
41
+ */
42
+ transport(message, options = {}) {
43
+ const configs = FactoryHelper.groupConfigs(options, this.configs)
44
+
45
+ message = FormatterFactory.fabricate(configs.formatter).format(
46
+ message,
47
+ configs.formatterConfig,
48
+ )
49
+
50
+ process[configs.streamType].write(`${message}\n`)
51
+ }
30
52
  }
31
- exports.ConsoleDriver = ConsoleDriver;
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  /**
3
2
  * @athenna/logger
4
3
  *
@@ -7,26 +6,49 @@
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
- Object.defineProperty(exports, "__esModule", { value: true });
11
- exports.DebugDriver = void 0;
12
- const debug_1 = require("debug");
13
- const utils_1 = require("@secjs/utils");
14
- const FormatterFactory_1 = require("../Factories/FormatterFactory");
15
- class DebugDriver {
16
- constructor(channel, configs = {}) {
17
- 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);
21
- }
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);
30
- }
9
+
10
+ import debug from 'debug'
11
+
12
+ import { Config } from '@secjs/utils'
13
+
14
+ import { FactoryHelper, FormatterFactory } from '#src/index'
15
+
16
+ export class DebugDriver {
17
+ /**
18
+ * Holds the configuration set of DebugDriver.
19
+ *
20
+ * @type {{ namespace?: string, formatter?: any, formatterConfig?: any }}
21
+ */
22
+ configs
23
+
24
+ /**
25
+ * Creates a new instance of DebugDriver.
26
+ *
27
+ * @param {string} channel
28
+ * @param {any} [configs]
29
+ * @return {DebugDriver}
30
+ */
31
+ constructor(channel, configs = {}) {
32
+ const channelConfig = Config.get(`logging.channels.${channel}`)
33
+
34
+ this.configs = FactoryHelper.groupConfigs(configs, channelConfig)
35
+ }
36
+
37
+ /**
38
+ * Transport the log.
39
+ *
40
+ * @param {string} message
41
+ * @param {{ namespace?: string, formatter?: any, formatterConfig?: any }} [options]
42
+ * @return {void}
43
+ */
44
+ transport(message, options = {}) {
45
+ const configs = FactoryHelper.groupConfigs(options, this.configs)
46
+
47
+ message = FormatterFactory.fabricate(configs.formatter).format(
48
+ message,
49
+ configs.formatterConfig,
50
+ )
51
+
52
+ debug(configs.namespace)(message)
53
+ }
31
54
  }
32
- exports.DebugDriver = DebugDriver;
@@ -0,0 +1,57 @@
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 axios from 'axios'
11
+
12
+ import { Config } from '@secjs/utils'
13
+
14
+ import { ColorHelper, FactoryHelper, FormatterFactory } from '#src/index'
15
+
16
+ export class DiscordDriver {
17
+ /**
18
+ * Holds the configuration set of DiscordDriver.
19
+ *
20
+ * @type {{ url?: string, username?: string, formatter?: any, formatterConfig?: any }}
21
+ */
22
+ configs
23
+
24
+ /**
25
+ * Creates a new instance of DiscordDriver.
26
+ *
27
+ * @param {string} channel
28
+ * @param {any} [configs]
29
+ * @return {DiscordDriver}
30
+ */
31
+ constructor(channel, configs = {}) {
32
+ const channelConfig = Config.get(`logging.channels.${channel}`)
33
+
34
+ this.configs = FactoryHelper.groupConfigs(configs, channelConfig)
35
+ }
36
+
37
+ /**
38
+ * Transport the log.
39
+ *
40
+ * @param {string} message
41
+ * @param {{ url?: string, username?: string, formatter?: any, formatterConfig?: any }} [options]
42
+ * @return {Promise<void>}
43
+ */
44
+ async transport(message, options = {}) {
45
+ const configs = FactoryHelper.groupConfigs(options, this.configs)
46
+
47
+ message = FormatterFactory.fabricate(configs.formatter).format(
48
+ message,
49
+ configs.formatterConfig,
50
+ )
51
+
52
+ await axios.post(configs.url, {
53
+ username: configs.username,
54
+ content: ColorHelper.removeColors(message),
55
+ })
56
+ }
57
+ }
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  /**
3
2
  * @athenna/logger
4
3
  *
@@ -7,38 +6,56 @@
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
- Object.defineProperty(exports, "__esModule", { value: true });
11
- exports.FileDriver = void 0;
12
- const path_1 = require("path");
13
- const utils_1 = require("@secjs/utils");
14
- const Color_1 = require("../Utils/Color");
15
- const fs_1 = require("fs");
16
- const FormatterFactory_1 = require("../Factories/FormatterFactory");
17
- class FileDriver {
18
- constructor(channel, configs = {}) {
19
- 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);
23
- }
24
- async transport(message, options) {
25
- options = Object.assign({}, {
26
- filePath: this._filePath,
27
- formatter: this._formatter,
28
- }, options);
29
- const filePath = options.filePath;
30
- const { dir } = path_1.parse(filePath);
31
- if (!fs_1.existsSync(dir)) {
32
- fs_1.mkdirSync(dir, { recursive: true });
33
- }
34
- const formatterOptions = Object.assign({}, this._formatterConfig, options.formatterConfig);
35
- message = FormatterFactory_1.FormatterFactory.fabricate(options.formatter).format(message, formatterOptions);
36
- return new Promise((resolve, reject) => {
37
- const stream = fs_1.createWriteStream(filePath, { flags: 'a' });
38
- stream.write(`${Color_1.Color.removeColors(message)}` + '\n');
39
- stream.on('error', reject);
40
- stream.end(resolve);
41
- });
9
+
10
+ import { Config, File } from '@secjs/utils'
11
+
12
+ import { ColorHelper, FactoryHelper, FormatterFactory } from '#src/index'
13
+
14
+ export class FileDriver {
15
+ /**
16
+ * Holds the configuration set of FileDriver.
17
+ *
18
+ * @type {{ filePath?: string, formatter?: any, formatterConfig?: any }}
19
+ */
20
+ configs
21
+
22
+ /**
23
+ * Creates a new instance of FileDriver.
24
+ *
25
+ * @param {string} channel
26
+ * @param {any} [configs]
27
+ * @return {FileDriver}
28
+ */
29
+ constructor(channel, configs = {}) {
30
+ const channelConfig = Config.get(`logging.channels.${channel}`)
31
+
32
+ this.configs = FactoryHelper.groupConfigs(configs, channelConfig)
33
+ }
34
+
35
+ /**
36
+ * Transport the log.
37
+ *
38
+ * @param {string} message
39
+ * @param {{ filePath?: string, formatter?: any, formatterConfig?: any }} [options]
40
+ * @return {Promise<void>}
41
+ */
42
+ async transport(message, options = {}) {
43
+ const configs = FactoryHelper.groupConfigs(options, this.configs)
44
+
45
+ message = FormatterFactory.fabricate(configs.formatter).format(
46
+ message,
47
+ configs.formatterConfig,
48
+ )
49
+
50
+ const messageBuffer = Buffer.from(`${ColorHelper.removeColors(message)}\n`)
51
+
52
+ const file = new File(configs.filePath, messageBuffer)
53
+ const fileExists = await File.exists(configs.filePath)
54
+
55
+ if (!fileExists) {
56
+ return file.load()
42
57
  }
58
+
59
+ return file.append(messageBuffer)
60
+ }
43
61
  }
44
- exports.FileDriver = FileDriver;
@@ -0,0 +1,28 @@
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 NullDriver {
11
+ /**
12
+ * Creates a new instance of NullDriver.
13
+ *
14
+ * @param {string} _channel
15
+ * @param {any} [_configs]
16
+ * @return {NullDriver}
17
+ */
18
+ constructor(_channel, _configs = {}) {}
19
+
20
+ /**
21
+ * Transport the log.
22
+ *
23
+ * @param {string} _message
24
+ * @param {any} [_options]
25
+ * @return {void}
26
+ */
27
+ transport(_message, _options) {}
28
+ }