@kawaiininja/logger 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,83 @@
1
+ # @kawaiininja/logger
2
+
3
+ A lightweight, customizable logger for TypeScript/JavaScript applications, featuring log levels, custom colors, and grouping capabilities.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @kawaiininja/logger
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ### Basic Usage
14
+
15
+ ```typescript
16
+ import { Logger } from "@kawaiininja/logger";
17
+
18
+ const logger = new Logger({
19
+ name: "MyArgs",
20
+ level: "debug",
21
+ });
22
+
23
+ logger.info("Application started");
24
+ logger.error("Something went wrong", { error: "details" });
25
+ ```
26
+
27
+ ### Configuration Options
28
+
29
+ You can configure the logger when initializing:
30
+
31
+ ```typescript
32
+ const logger = new Logger({
33
+ // Name to display in the log prefix (default: "App")
34
+ name: "AuthService",
35
+
36
+ // Minimum log level to display (default: "error")
37
+ // Levels: "debug" | "info" | "warn" | "error" | "off"
38
+ level: "debug",
39
+
40
+ // Force development mode (default: false)
41
+ // If true, enables debug logs regardless of other settings
42
+ isDevelopment: process.env.NODE_ENV === "development",
43
+
44
+ // Override default colors (optional)
45
+ colors: {
46
+ info: "#3b82f6",
47
+ error: "#ef4444",
48
+ },
49
+ });
50
+ ```
51
+
52
+ ### Log Levels
53
+
54
+ The logger supports the following levels in order of priority:
55
+
56
+ 1. `error` (High priority)
57
+ 2. `warn`
58
+ 3. `info`
59
+ 4. `debug` (Low priority)
60
+
61
+ Setting certain level will show logs of that level and higher. For example, setting `level: 'warn'` will show `warn` and `error` logs, but hide `info` and `debug`.
62
+
63
+ ### Advanced Features
64
+
65
+ #### Grouping Logs
66
+
67
+ ```typescript
68
+ logger.group("User Details");
69
+ logger.info("Name: Myu");
70
+ logger.info("Role: Admin");
71
+ logger.groupEnd();
72
+ ```
73
+
74
+ #### Table Logging
75
+
76
+ ```typescript
77
+ const users = [
78
+ { id: 1, name: "Alice" },
79
+ { id: 2, name: "Bob" },
80
+ ];
81
+
82
+ logger.table(users);
83
+ ```
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Logger class for application logging
3
+ * Based on variant.v2/logger.util.js from frontend-libs
4
+ */
5
+ export interface LoggerOptions {
6
+ name?: string;
7
+ level?: string;
8
+ isDevelopment?: boolean;
9
+ colors?: Partial<Record<LogLevel, string>>;
10
+ }
11
+ type LogLevel = "debug" | "info" | "warn" | "error" | "off";
12
+ export declare class Logger {
13
+ private name;
14
+ private levels;
15
+ private currentLevel;
16
+ private isDevelopment;
17
+ private colors;
18
+ constructor(options?: LoggerOptions);
19
+ private shouldLog;
20
+ private log;
21
+ debug(...args: any[]): void;
22
+ info(...args: any[]): void;
23
+ warn(...args: any[]): void;
24
+ error(...args: any[]): void;
25
+ /**
26
+ * Start a collapsible group
27
+ */
28
+ group(title: string, level?: LogLevel): void;
29
+ /**
30
+ * End a collapsible group
31
+ */
32
+ groupEnd(): void;
33
+ /**
34
+ * Log data as a table
35
+ */
36
+ table(data: any, level?: LogLevel): void;
37
+ }
38
+ export {};
package/dist/index.js ADDED
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+ /**
3
+ * Logger class for application logging
4
+ * Based on variant.v2/logger.util.js from frontend-libs
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.Logger = void 0;
8
+ class Logger {
9
+ constructor(options = {}) {
10
+ this.name = options.name || "App";
11
+ this.levels = { debug: 4, info: 3, warn: 2, error: 1, off: 0 };
12
+ const requestedLevel = options.level?.toLowerCase();
13
+ this.currentLevel =
14
+ requestedLevel && this.levels[requestedLevel] !== undefined
15
+ ? this.levels[requestedLevel]
16
+ : this.levels.error;
17
+ this.isDevelopment = options.isDevelopment ?? false;
18
+ this.colors = {
19
+ debug: "#38BDF8",
20
+ info: "#10B981",
21
+ warn: "#FBBF24",
22
+ error: "#F87171",
23
+ ...options.colors,
24
+ };
25
+ }
26
+ shouldLog(level) {
27
+ if (!this.isDevelopment && level === "debug")
28
+ return false;
29
+ const req = this.levels[level.toLowerCase()] || 0;
30
+ return req > 0 && req <= this.currentLevel;
31
+ }
32
+ log(level, ...args) {
33
+ if (!this.shouldLog(level))
34
+ return;
35
+ const prefix = `%c[${this.name}] ${level.toUpperCase()}`;
36
+ const style = `color: ${this.colors[level]}; font-weight: bold;`;
37
+ const method = console[level] || console.log;
38
+ method(prefix, style, ...args);
39
+ }
40
+ debug(...args) {
41
+ this.log("debug", ...args);
42
+ }
43
+ info(...args) {
44
+ this.log("info", ...args);
45
+ }
46
+ warn(...args) {
47
+ this.log("warn", ...args);
48
+ }
49
+ error(...args) {
50
+ this.log("error", ...args);
51
+ }
52
+ /**
53
+ * Start a collapsible group
54
+ */
55
+ group(title, level = "info") {
56
+ if (!this.shouldLog(level))
57
+ return;
58
+ console.group(`%c${title}`, "color: #10B981; font-weight: bold;");
59
+ }
60
+ /**
61
+ * End a collapsible group
62
+ */
63
+ groupEnd() {
64
+ console.groupEnd();
65
+ }
66
+ /**
67
+ * Log data as a table
68
+ */
69
+ table(data, level = "debug") {
70
+ if (!this.shouldLog(level))
71
+ return;
72
+ console.table(data);
73
+ }
74
+ }
75
+ exports.Logger = Logger;
package/package.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "@kawaiininja/logger",
3
+ "version": "1.0.2",
4
+ "description": "Logger class for application logging",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "scripts": {
8
+ "build": "tsc"
9
+ },
10
+ "keywords": [
11
+ "logger",
12
+ "logging",
13
+ "typescript"
14
+ ],
15
+ "author": "",
16
+ "license": "ISC",
17
+ "devDependencies": {
18
+ "typescript": "^5.3.0"
19
+ }
20
+ }