@carlos-tzin/tzin 0.1.9 → 0.1.10
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/log.d.ts +51 -0
- package/dist/log.js +94 -0
- package/package.json +5 -1
package/dist/log.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
export type LogLevel = 'debug' | 'info' | 'warn' | 'error' | 'fatal';
|
|
2
|
+
export interface LogEntry {
|
|
3
|
+
level: LogLevel;
|
|
4
|
+
message: string;
|
|
5
|
+
data?: Record<string, unknown>;
|
|
6
|
+
timestamp: Date;
|
|
7
|
+
context?: string;
|
|
8
|
+
}
|
|
9
|
+
export interface Logger {
|
|
10
|
+
debug(message: string, data?: Record<string, unknown>): void;
|
|
11
|
+
info(message: string, data?: Record<string, unknown>): void;
|
|
12
|
+
warn(message: string, data?: Record<string, unknown>): void;
|
|
13
|
+
error(message: string, data?: Record<string, unknown>): void;
|
|
14
|
+
fatal(message: string, data?: Record<string, unknown>): void;
|
|
15
|
+
/** Create a child logger with a prefix */
|
|
16
|
+
child(prefix: string): Logger;
|
|
17
|
+
}
|
|
18
|
+
export interface LoggerConfig {
|
|
19
|
+
/** Minimum log level (default: 'info') */
|
|
20
|
+
level?: LogLevel;
|
|
21
|
+
/** Custom transport function */
|
|
22
|
+
transport?: (entry: LogEntry) => void;
|
|
23
|
+
/** Include timestamp in output (default: true) */
|
|
24
|
+
timestamp?: boolean;
|
|
25
|
+
/** Pretty print with colors (default: true in dev) */
|
|
26
|
+
pretty?: boolean;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Configure the global logger.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* import { configure } from '@carlos-tzin/tzin/log'
|
|
34
|
+
*
|
|
35
|
+
* configure({ level: 'debug', pretty: true })
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export declare function configure(config: LoggerConfig): void;
|
|
39
|
+
/**
|
|
40
|
+
* Get the global logger.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* import { log } from '@carlos-tzin/tzin/log'
|
|
45
|
+
*
|
|
46
|
+
* log.info('Server started', { port: 3000 })
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export declare function getLogger(context?: string): Logger;
|
|
50
|
+
/** Convenience export */
|
|
51
|
+
export declare const log: Logger;
|
package/dist/log.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
// ── Types ────────────────────────────────────────────────────────────
|
|
2
|
+
// ── Log Level Order ──────────────────────────────────────────────────
|
|
3
|
+
const LOG_LEVELS = {
|
|
4
|
+
debug: 0,
|
|
5
|
+
info: 1,
|
|
6
|
+
warn: 2,
|
|
7
|
+
error: 3,
|
|
8
|
+
fatal: 4,
|
|
9
|
+
};
|
|
10
|
+
// ── Colors ───────────────────────────────────────────────────────────
|
|
11
|
+
const COLORS = {
|
|
12
|
+
debug: '\x1b[36m', // cyan
|
|
13
|
+
info: '\x1b[32m', // green
|
|
14
|
+
warn: '\x1b[33m', // yellow
|
|
15
|
+
error: '\x1b[31m', // red
|
|
16
|
+
fatal: '\x1b[35m', // magenta
|
|
17
|
+
};
|
|
18
|
+
const RESET = '\x1b[0m';
|
|
19
|
+
// ── Default Transport ────────────────────────────────────────────────
|
|
20
|
+
function defaultTransport(entry, config) {
|
|
21
|
+
const { level, message, data, timestamp, context } = entry;
|
|
22
|
+
const showTimestamp = config.timestamp !== false;
|
|
23
|
+
const pretty = config.pretty !== false;
|
|
24
|
+
if (pretty) {
|
|
25
|
+
const color = COLORS[level];
|
|
26
|
+
const prefix = context ? `[${context}]` : '';
|
|
27
|
+
const time = showTimestamp ? `${timestamp.toISOString()} ` : '';
|
|
28
|
+
const dataStr = data && Object.keys(data).length > 0 ? ` ${JSON.stringify(data)}` : '';
|
|
29
|
+
console.log(`${color}${time}${prefix} ${level.toUpperCase()}${RESET} ${message}${dataStr}`);
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
const json = { level, message, timestamp: timestamp.toISOString() };
|
|
33
|
+
if (context)
|
|
34
|
+
json.context = context;
|
|
35
|
+
if (data && Object.keys(data).length > 0)
|
|
36
|
+
json.data = data;
|
|
37
|
+
console.log(JSON.stringify(json));
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
// ── Logger Factory ───────────────────────────────────────────────────
|
|
41
|
+
function createLogger(config, context) {
|
|
42
|
+
const minLevel = LOG_LEVELS[config.level ?? 'info'];
|
|
43
|
+
const transport = config.transport ?? ((entry) => defaultTransport(entry, config));
|
|
44
|
+
function log(level, message, data) {
|
|
45
|
+
if (LOG_LEVELS[level] < minLevel)
|
|
46
|
+
return;
|
|
47
|
+
const entry = {
|
|
48
|
+
level,
|
|
49
|
+
message,
|
|
50
|
+
data,
|
|
51
|
+
timestamp: new Date(),
|
|
52
|
+
context,
|
|
53
|
+
};
|
|
54
|
+
transport(entry);
|
|
55
|
+
}
|
|
56
|
+
return {
|
|
57
|
+
debug: (msg, data) => log('debug', msg, data),
|
|
58
|
+
info: (msg, data) => log('info', msg, data),
|
|
59
|
+
warn: (msg, data) => log('warn', msg, data),
|
|
60
|
+
error: (msg, data) => log('error', msg, data),
|
|
61
|
+
fatal: (msg, data) => log('fatal', msg, data),
|
|
62
|
+
child: (prefix) => createLogger(config, context ? `${context}:${prefix}` : prefix),
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
// ── Global Logger ────────────────────────────────────────────────────
|
|
66
|
+
let globalConfig = {};
|
|
67
|
+
/**
|
|
68
|
+
* Configure the global logger.
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```ts
|
|
72
|
+
* import { configure } from '@carlos-tzin/tzin/log'
|
|
73
|
+
*
|
|
74
|
+
* configure({ level: 'debug', pretty: true })
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
export function configure(config) {
|
|
78
|
+
globalConfig = config;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Get the global logger.
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```ts
|
|
85
|
+
* import { log } from '@carlos-tzin/tzin/log'
|
|
86
|
+
*
|
|
87
|
+
* log.info('Server started', { port: 3000 })
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
export function getLogger(context) {
|
|
91
|
+
return createLogger(globalConfig, context);
|
|
92
|
+
}
|
|
93
|
+
/** Convenience export */
|
|
94
|
+
export const log = getLogger();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@carlos-tzin/tzin",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.10",
|
|
4
4
|
"description": "Contract-first TypeScript framework. Types that scale, realtime channels with presence, and an MCP server for every API.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "The tzin authors",
|
|
@@ -58,6 +58,10 @@
|
|
|
58
58
|
"./jobs": {
|
|
59
59
|
"types": "./dist/jobs.d.ts",
|
|
60
60
|
"default": "./dist/jobs.js"
|
|
61
|
+
},
|
|
62
|
+
"./log": {
|
|
63
|
+
"types": "./dist/log.d.ts",
|
|
64
|
+
"default": "./dist/log.js"
|
|
61
65
|
}
|
|
62
66
|
},
|
|
63
67
|
"files": [
|