@gratheon/log-lib 2.0.1 → 2.1.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.
- package/dist/logger.d.ts +1 -1
- package/dist/logger.js +28 -7
- package/dist/types.d.ts +3 -1
- package/package.json +1 -1
- package/src/logger.ts +34 -8
- package/src/types.ts +4 -1
package/dist/logger.d.ts
CHANGED
package/dist/logger.js
CHANGED
|
@@ -33,8 +33,15 @@ const fs = __importStar(require("fs"));
|
|
|
33
33
|
const path = __importStar(require("path"));
|
|
34
34
|
let conn = null;
|
|
35
35
|
let dbInitialized = false;
|
|
36
|
+
const LOG_LEVELS = {
|
|
37
|
+
debug: 0,
|
|
38
|
+
info: 1,
|
|
39
|
+
warn: 2,
|
|
40
|
+
error: 3
|
|
41
|
+
};
|
|
42
|
+
let currentLogLevel = LOG_LEVELS.info;
|
|
36
43
|
async function initializeConnection(config) {
|
|
37
|
-
if (dbInitialized)
|
|
44
|
+
if (dbInitialized || !config.mysql)
|
|
38
45
|
return;
|
|
39
46
|
try {
|
|
40
47
|
const database = config.mysql.database || 'logs';
|
|
@@ -43,7 +50,7 @@ async function initializeConnection(config) {
|
|
|
43
50
|
connectionString: `mysql://${config.mysql.user}:${config.mysql.password}@${config.mysql.host}:${config.mysql.port}/?connectionLimit=1&waitForConnections=true`,
|
|
44
51
|
bigIntMode: 'number',
|
|
45
52
|
});
|
|
46
|
-
await tempConn.query((0, mysql_1.sql) `CREATE DATABASE IF NOT EXISTS
|
|
53
|
+
await tempConn.query((0, mysql_1.sql) `CREATE DATABASE IF NOT EXISTS ${mysql_1.sql.ident(database)} CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;`);
|
|
47
54
|
await tempConn.dispose();
|
|
48
55
|
// Now create the main connection pool with the logs database
|
|
49
56
|
conn = (0, mysql_1.default)({
|
|
@@ -82,6 +89,12 @@ async function initializeConnection(config) {
|
|
|
82
89
|
}
|
|
83
90
|
}
|
|
84
91
|
function log(level, message, meta) {
|
|
92
|
+
// Check if this log level should be filtered
|
|
93
|
+
const levelKey = level.replace(/\x1b\[\d+m/g, ''); // Remove ANSI codes for comparison
|
|
94
|
+
const messageLevel = LOG_LEVELS[levelKey];
|
|
95
|
+
if (messageLevel !== undefined && messageLevel < currentLogLevel) {
|
|
96
|
+
return; // Skip logging this message
|
|
97
|
+
}
|
|
85
98
|
let time = new Date().toISOString();
|
|
86
99
|
let hhMMTime = time.slice(11, 19);
|
|
87
100
|
// colorize time to have ansi blue color
|
|
@@ -254,11 +267,19 @@ function storeInDB(level, message, meta) {
|
|
|
254
267
|
console.error('Unexpected failure preparing log for DB', e);
|
|
255
268
|
}
|
|
256
269
|
}
|
|
257
|
-
function createLogger(config) {
|
|
258
|
-
//
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
270
|
+
function createLogger(config = {}) {
|
|
271
|
+
// Set up log level filtering
|
|
272
|
+
// Priority: 1) config.logLevel, 2) process.env.LOG_LEVEL, 3) default based on ENV_ID
|
|
273
|
+
const configuredLevel = config.logLevel ||
|
|
274
|
+
process.env.LOG_LEVEL ||
|
|
275
|
+
(process.env.ENV_ID === 'dev' ? 'debug' : 'info');
|
|
276
|
+
currentLogLevel = LOG_LEVELS[configuredLevel] ?? LOG_LEVELS.info;
|
|
277
|
+
// Start initialization asynchronously but don't wait for it (only if MySQL config provided)
|
|
278
|
+
if (config.mysql) {
|
|
279
|
+
initializeConnection(config).catch(err => {
|
|
280
|
+
console.error('Error during log database initialization:', err);
|
|
281
|
+
});
|
|
282
|
+
}
|
|
262
283
|
const logger = {
|
|
263
284
|
info: (message, meta) => {
|
|
264
285
|
const metaObj = safeMeta(meta);
|
package/dist/types.d.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
|
+
export type LogLevel = 'debug' | 'info' | 'warn' | 'error';
|
|
1
2
|
export interface LoggerConfig {
|
|
2
|
-
mysql
|
|
3
|
+
mysql?: {
|
|
3
4
|
host: string;
|
|
4
5
|
port: number;
|
|
5
6
|
user: string;
|
|
6
7
|
password: string;
|
|
7
8
|
database?: string;
|
|
8
9
|
};
|
|
10
|
+
logLevel?: LogLevel;
|
|
9
11
|
}
|
|
10
12
|
export interface LogMetadata {
|
|
11
13
|
[key: string]: any;
|
package/package.json
CHANGED
package/src/logger.ts
CHANGED
|
@@ -2,13 +2,22 @@ import createConnectionPool, { sql, ConnectionPool } from "@databases/mysql";
|
|
|
2
2
|
import jsonStringify from "fast-safe-stringify";
|
|
3
3
|
import * as fs from 'fs';
|
|
4
4
|
import * as path from 'path';
|
|
5
|
-
import { LoggerConfig, Logger, FastifyLogger, LogMetadata } from "./types";
|
|
5
|
+
import { LoggerConfig, Logger, FastifyLogger, LogMetadata, LogLevel } from "./types";
|
|
6
6
|
|
|
7
7
|
let conn: ConnectionPool | null = null;
|
|
8
8
|
let dbInitialized = false;
|
|
9
9
|
|
|
10
|
+
const LOG_LEVELS: Record<LogLevel, number> = {
|
|
11
|
+
debug: 0,
|
|
12
|
+
info: 1,
|
|
13
|
+
warn: 2,
|
|
14
|
+
error: 3
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
let currentLogLevel: number = LOG_LEVELS.info;
|
|
18
|
+
|
|
10
19
|
async function initializeConnection(config: LoggerConfig) {
|
|
11
|
-
if (dbInitialized) return;
|
|
20
|
+
if (dbInitialized || !config.mysql) return;
|
|
12
21
|
|
|
13
22
|
try {
|
|
14
23
|
const database = config.mysql.database || 'logs';
|
|
@@ -19,7 +28,7 @@ async function initializeConnection(config: LoggerConfig) {
|
|
|
19
28
|
bigIntMode: 'number',
|
|
20
29
|
});
|
|
21
30
|
|
|
22
|
-
await tempConn.query(sql`CREATE DATABASE IF NOT EXISTS
|
|
31
|
+
await tempConn.query(sql`CREATE DATABASE IF NOT EXISTS ${sql.ident(database)} CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;`);
|
|
23
32
|
await tempConn.dispose();
|
|
24
33
|
|
|
25
34
|
// Now create the main connection pool with the logs database
|
|
@@ -61,6 +70,13 @@ async function initializeConnection(config: LoggerConfig) {
|
|
|
61
70
|
}
|
|
62
71
|
|
|
63
72
|
function log(level: string, message: string, meta?: any) {
|
|
73
|
+
// Check if this log level should be filtered
|
|
74
|
+
const levelKey = level.replace(/\x1b\[\d+m/g, '') as LogLevel; // Remove ANSI codes for comparison
|
|
75
|
+
const messageLevel = LOG_LEVELS[levelKey];
|
|
76
|
+
if (messageLevel !== undefined && messageLevel < currentLogLevel) {
|
|
77
|
+
return; // Skip logging this message
|
|
78
|
+
}
|
|
79
|
+
|
|
64
80
|
let time = new Date().toISOString();
|
|
65
81
|
let hhMMTime = time.slice(11, 19);
|
|
66
82
|
// colorize time to have ansi blue color
|
|
@@ -228,11 +244,21 @@ function storeInDB(level: string, message: any, meta?: any) {
|
|
|
228
244
|
}
|
|
229
245
|
}
|
|
230
246
|
|
|
231
|
-
export function createLogger(config: LoggerConfig): { logger: Logger; fastifyLogger: FastifyLogger } {
|
|
232
|
-
//
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
247
|
+
export function createLogger(config: LoggerConfig = {}): { logger: Logger; fastifyLogger: FastifyLogger } {
|
|
248
|
+
// Set up log level filtering
|
|
249
|
+
// Priority: 1) config.logLevel, 2) process.env.LOG_LEVEL, 3) default based on ENV_ID
|
|
250
|
+
const configuredLevel = config.logLevel ||
|
|
251
|
+
(process.env.LOG_LEVEL as LogLevel) ||
|
|
252
|
+
(process.env.ENV_ID === 'dev' ? 'debug' : 'info');
|
|
253
|
+
|
|
254
|
+
currentLogLevel = LOG_LEVELS[configuredLevel] ?? LOG_LEVELS.info;
|
|
255
|
+
|
|
256
|
+
// Start initialization asynchronously but don't wait for it (only if MySQL config provided)
|
|
257
|
+
if (config.mysql) {
|
|
258
|
+
initializeConnection(config).catch(err => {
|
|
259
|
+
console.error('Error during log database initialization:', err);
|
|
260
|
+
});
|
|
261
|
+
}
|
|
236
262
|
|
|
237
263
|
const logger: Logger = {
|
|
238
264
|
info: (message: string, meta?: LogMetadata) => {
|
package/src/types.ts
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
|
+
export type LogLevel = 'debug' | 'info' | 'warn' | 'error';
|
|
2
|
+
|
|
1
3
|
export interface LoggerConfig {
|
|
2
|
-
mysql
|
|
4
|
+
mysql?: {
|
|
3
5
|
host: string;
|
|
4
6
|
port: number;
|
|
5
7
|
user: string;
|
|
6
8
|
password: string;
|
|
7
9
|
database?: string; // defaults to 'logs'
|
|
8
10
|
};
|
|
11
|
+
logLevel?: LogLevel; // defaults to 'info' in production, 'debug' in dev
|
|
9
12
|
}
|
|
10
13
|
|
|
11
14
|
export interface LogMetadata {
|