@f3rm1/logger 1.0.0 → 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.
@@ -0,0 +1,67 @@
1
+ import chalk from 'chalk';
2
+
3
+ declare const ROLES: {
4
+ info: {
5
+ label: string;
6
+ color: chalk.Chalk;
7
+ type: "log";
8
+ };
9
+ success: {
10
+ label: string;
11
+ color: chalk.Chalk;
12
+ type: "log";
13
+ };
14
+ warn: {
15
+ label: string;
16
+ color: chalk.Chalk;
17
+ type: "warn";
18
+ };
19
+ error: {
20
+ label: string;
21
+ color: chalk.Chalk;
22
+ type: "error";
23
+ };
24
+ debug: {
25
+ label: string;
26
+ color: chalk.Chalk;
27
+ type: "log";
28
+ };
29
+ start: {
30
+ label: string;
31
+ color: chalk.Chalk;
32
+ type: "log";
33
+ };
34
+ stop: {
35
+ label: string;
36
+ color: chalk.Chalk;
37
+ type: "log";
38
+ };
39
+ wait: {
40
+ label: string;
41
+ color: chalk.Chalk;
42
+ type: "log";
43
+ };
44
+ event: {
45
+ label: string;
46
+ color: chalk.Chalk;
47
+ type: "log";
48
+ };
49
+ };
50
+ type RolesMethods = keyof typeof ROLES;
51
+
52
+ type LogMethod = (message: string) => void;
53
+ type LoggerInstance = LogMethod & Record<RolesMethods, LogMethod>;
54
+ interface LoggerOptions {
55
+ group?: string;
56
+ }
57
+ type LoggerArgs = string | LoggerOptions;
58
+ /**
59
+ * Creates logger instance for reusable group calls. Usage:
60
+ * ```js
61
+ * const logger = createLogger('Console');
62
+ * ```
63
+ */
64
+ declare function createLogger(args?: LoggerArgs): LoggerInstance;
65
+ declare const logger: LoggerInstance;
66
+
67
+ export { createLogger, logger as default, logger };
package/dist/logger.js ADDED
@@ -0,0 +1,92 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // logger.ts
21
+ var logger_exports = {};
22
+ __export(logger_exports, {
23
+ createLogger: () => createLogger,
24
+ default: () => logger_default,
25
+ logger: () => logger
26
+ });
27
+ module.exports = __toCommonJS(logger_exports);
28
+
29
+ // roles.ts
30
+ var chalk = require("chalk");
31
+ var ROLES = {
32
+ info: { label: "[INFO]", color: chalk.cyan, type: "log" },
33
+ success: { label: "[SUCCESS]", color: chalk.green, type: "log" },
34
+ warn: { label: "[WARN]", color: chalk.yellow, type: "warn" },
35
+ error: { label: "[ERROR]", color: chalk.red.bold, type: "error" },
36
+ debug: { label: "[DEBUG]", color: chalk.magenta, type: "log" },
37
+ start: { label: "[START]", color: chalk.green.bold, type: "log" },
38
+ stop: { label: "[STOP]", color: chalk.red, type: "log" },
39
+ wait: { label: "[WAIT]", color: chalk.blue, type: "log" },
40
+ event: { label: "[EVENT]", color: chalk.cyan.bold, type: "log" }
41
+ };
42
+ var DEFAULT_ROLE = Object.keys(ROLES)[0];
43
+
44
+ // helpers/get-time-label.ts
45
+ var chalk2 = require("chalk");
46
+ function getTimeLabel() {
47
+ return chalk2.gray(`[${(/* @__PURE__ */ new Date()).toLocaleTimeString()}]`);
48
+ }
49
+
50
+ // constants.ts
51
+ var MAX_PREFIX_WIDTH = Math.max(
52
+ ...Object.values(ROLES).map((cfg) => cfg.label.length)
53
+ );
54
+
55
+ // helpers/get-label-with-padding.ts
56
+ var chalk3 = require("chalk");
57
+ function getLabelWithPadding(label, colorFn) {
58
+ return colorFn(label.padEnd(MAX_PREFIX_WIDTH));
59
+ }
60
+
61
+ // helpers/get-log-body.ts
62
+ var chalk4 = require("chalk");
63
+ function getLogBody(label, colorFn, message, group) {
64
+ const groupPart = group ? " " + chalk4.bgBlack.whiteBright.bold(` ${group} `) : "";
65
+ return `${getTimeLabel()}${groupPart} ${getLabelWithPadding(label, colorFn)} ${colorFn(message)}`;
66
+ }
67
+
68
+ // logger.ts
69
+ function buildLogger(group = "") {
70
+ const instance = ((message) => {
71
+ instance[DEFAULT_ROLE](message);
72
+ });
73
+ for (const role of Object.keys(ROLES)) {
74
+ const cfg = ROLES[role];
75
+ instance[role] = (message) => {
76
+ console[cfg.type](getLogBody(cfg.label, cfg.color, message, group));
77
+ };
78
+ }
79
+ return instance;
80
+ }
81
+ function createLogger(args = "") {
82
+ var _a;
83
+ const group = typeof args === "string" ? args : (_a = args.group) != null ? _a : "";
84
+ return buildLogger(group);
85
+ }
86
+ var logger = createLogger();
87
+ var logger_default = logger;
88
+ // Annotate the CommonJS export names for ESM import in node:
89
+ 0 && (module.exports = {
90
+ createLogger,
91
+ logger
92
+ });
package/package.json CHANGED
@@ -1,22 +1,35 @@
1
1
  {
2
2
  "name": "@f3rm1/logger",
3
- "version": "1.0.0",
3
+ "version": "1.2.0",
4
4
  "description": "",
5
5
  "main": "logger.js",
6
6
  "scripts": {
7
- "patch": "npm version patch && npm publish"
7
+ "build": "tsup logger.ts --dts --clean",
8
+ "test": "tsup ./tests/logger-test.ts && node ./dist/logger-test.js",
9
+ "patch": "npm version patch && npm publish",
10
+ "minor": "npm version minor && npm publish",
11
+ "major": "npm version major && npm publish"
8
12
  },
9
13
  "keywords": [],
10
14
  "author": "",
11
15
  "license": "ISC",
12
16
  "type": "commonjs",
17
+ "files": [
18
+ "dist"
19
+ ],
13
20
  "publishConfig": {
14
21
  "access": "public"
15
22
  },
16
23
  "dependencies": {
17
- "chalk": "^4.1.2"
24
+ "chalk": "^4.1.2",
25
+ "typescript": "^6.0.2"
18
26
  },
19
27
  "peerDependencies": {
20
28
  "chalk": "^4.1.2"
29
+ },
30
+ "devDependencies": {
31
+ "@types/log-update": "^2.0.0",
32
+ "@types/node": "^25.6.0",
33
+ "tsup": "^8.5.1"
21
34
  }
22
35
  }
@@ -1,12 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <module type="WEB_MODULE" version="4">
3
- <component name="NewModuleRootManager">
4
- <content url="file://$MODULE_DIR$">
5
- <excludeFolder url="file://$MODULE_DIR$/.tmp" />
6
- <excludeFolder url="file://$MODULE_DIR$/temp" />
7
- <excludeFolder url="file://$MODULE_DIR$/tmp" />
8
- </content>
9
- <orderEntry type="inheritedJdk" />
10
- <orderEntry type="sourceFolder" forTests="false" />
11
- </component>
12
- </module>
package/.idea/misc.xml DELETED
@@ -1,10 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="MaterialThemeProjectNewConfig">
4
- <option name="metadata">
5
- <MTProjectMetadataState>
6
- <option name="userId" value="2aa97953:1962e9c7577:-7ffd" />
7
- </MTProjectMetadataState>
8
- </option>
9
- </component>
10
- </project>
package/.idea/modules.xml DELETED
@@ -1,8 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="ProjectModuleManager">
4
- <modules>
5
- <module fileurl="file://$PROJECT_DIR$/.idea/@f3rm1-logger.iml" filepath="$PROJECT_DIR$/.idea/@f3rm1-logger.iml" />
6
- </modules>
7
- </component>
8
- </project>
package/.idea/vcs.xml DELETED
@@ -1,6 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="VcsDirectoryMappings">
4
- <mapping directory="$PROJECT_DIR$" vcs="Git" />
5
- </component>
6
- </project>
package/logger.js DELETED
@@ -1,89 +0,0 @@
1
- const chalk = require('chalk');
2
-
3
- // config
4
- const LOG_LEVELS = {
5
- info: {
6
- label: '[INFO]',
7
- color: chalk.cyan,
8
- },
9
- success: {
10
- label: '[SUCCESS]',
11
- color: chalk.green,
12
- },
13
- warn: {
14
- label: '[WARN]',
15
- color: chalk.yellow,
16
- type: 'warn',
17
- },
18
- error: {
19
- label: '[ERROR]',
20
- color: chalk.red.bold,
21
- },
22
- debug: {
23
- label: '[DEBUG]',
24
- color: chalk.magenta,
25
- },
26
- start: {
27
- label: '[START]',
28
- color: chalk.green.bold,
29
- },
30
- stop: {
31
- label: '[STOP]',
32
- color: chalk.red,
33
- },
34
- wait: {
35
- label: '[WAIT]',
36
- color: chalk.blue,
37
- },
38
- event: {
39
- label: '[EVENT]',
40
- color: chalk.cyan.bold,
41
- },
42
- };
43
-
44
- // auto creating methods for logger
45
- const logger = createLoggerMethods();
46
- /**
47
- * Creates your own logger instance with custom preferences.
48
- */
49
- class Logger {
50
- constructor(prefix) {
51
- this.prefix = prefix;
52
- Object.assign(this, createLoggerMethods(prefix));
53
- }
54
- }
55
-
56
- module.exports = { logger, Logger };
57
-
58
- const MAX_LABEL_WIDTH = Math.max(...Object.values(LOG_LEVELS).map(level => level.label.length));
59
-
60
- /**
61
- * Creates logger methods. Can accept prefix for Logger instance.
62
- */
63
- function createLoggerMethods(prefix = '') {
64
- const methods = {};
65
-
66
- Object.entries(LOG_LEVELS).forEach(([level, config]) => {
67
- methods[level] = (message, ...args) => {
68
- const logMethod = /warn|error/.test(config.type) ? console[config.type] : console.log;
69
- logMethod(logBody(config.label, config.color, config.color(message), prefix), ...args);
70
- };
71
- });
72
-
73
- return methods;
74
- }
75
-
76
- function getTimeLabel() {
77
- const now = new Date();
78
- return chalk.gray(`[${now.toLocaleTimeString()}]`);
79
- }
80
-
81
- function padLabel(label, colorFn) {
82
- // adding padding and only after adding a color
83
- const paddedLabel = label.padEnd(MAX_LABEL_WIDTH, ' ');
84
- return colorFn(paddedLabel);
85
- }
86
-
87
- function logBody(label, colorFn, message, prefix) {
88
- return `${getTimeLabel()}${prefix ? ' ' + chalk.bgBlack.whiteBright.bold(` ${prefix} `) : ''} ${padLabel(label, colorFn)} ${message}`;
89
- }