jsonl-logger 0.1.0 → 0.1.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 +13 -13
- package/dist/google-cloud-logging.d.ts +2 -0
- package/dist/google-cloud-logging.js +1 -0
- package/dist/index-5vvjt922.js +2 -0
- package/dist/{index-rhgqp0zd.js → index-c56gnbme.js} +1 -1
- package/dist/{index-rxqh9v58.js → index-jvj9ywjg.js} +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/intercept.js +1 -1
- package/dist/preload.js +1 -1
- package/dist/types.d.ts +1 -0
- package/dist/victoria-logs.d.ts +1 -1
- package/dist/victoria-logs.js +1 -1
- package/package.json +6 -6
- package/src/{google-cloud.ts → google-cloud-logging.ts} +1 -1
- package/src/index.ts +3 -2
- package/src/intercept.ts +2 -2
- package/src/preload.ts +9 -8
- package/src/types.ts +2 -0
- package/src/victoria-logs.ts +1 -1
- package/dist/google-cloud.d.ts +0 -2
- package/dist/google-cloud.js +0 -1
- package/dist/index-x1ss6ry6.js +0 -2
package/README.md
CHANGED
|
@@ -23,20 +23,20 @@ logger.error('Request failed', { path: '/api' }, new Error('timeout'))
|
|
|
23
23
|
|
|
24
24
|
Two built-in formatters for popular log backends:
|
|
25
25
|
|
|
26
|
-
### Google Cloud Logging
|
|
26
|
+
### Google Cloud Logging (default)
|
|
27
27
|
|
|
28
28
|
```typescript
|
|
29
|
-
import {
|
|
30
|
-
import { Logger } from 'jsonl-logger'
|
|
31
|
-
|
|
32
|
-
const logger = new Logger(undefined, { json: true, formatter: googleCloud })
|
|
29
|
+
import { logger } from 'jsonl-logger'
|
|
33
30
|
// Output: {"message":"...","timestamp":"...","severity":"INFO",...}
|
|
34
31
|
```
|
|
35
32
|
|
|
36
|
-
### VictoriaLogs
|
|
33
|
+
### VictoriaLogs
|
|
37
34
|
|
|
38
35
|
```typescript
|
|
39
|
-
import {
|
|
36
|
+
import { VictoriaLogs } from 'jsonl-logger/victoria-logs'
|
|
37
|
+
import { Logger } from 'jsonl-logger'
|
|
38
|
+
|
|
39
|
+
const logger = new Logger(undefined, { json: true, formatter: VictoriaLogs })
|
|
40
40
|
// Output: {"_msg":"...","_time":"...","level":"info",...}
|
|
41
41
|
```
|
|
42
42
|
|
|
@@ -64,8 +64,8 @@ Monkey-patch `console.*` methods to output structured JSON — captures logs fro
|
|
|
64
64
|
import { intercept, originalConsole } from 'jsonl-logger/intercept'
|
|
65
65
|
|
|
66
66
|
intercept({
|
|
67
|
-
// Optional: custom formatter (default:
|
|
68
|
-
formatter:
|
|
67
|
+
// Optional: custom formatter (default: GoogleCloudLogging)
|
|
68
|
+
formatter: VictoriaLogs,
|
|
69
69
|
// Optional: filter out noisy messages
|
|
70
70
|
filter: (level, message) => !message.includes('deprecation'),
|
|
71
71
|
// Optional: minimum log level
|
|
@@ -86,7 +86,7 @@ bun --preload jsonl-logger/preload server.js
|
|
|
86
86
|
```
|
|
87
87
|
|
|
88
88
|
Environment variables:
|
|
89
|
-
- `LOG_FORMAT` — `google-cloud` or `victoria-logs`
|
|
89
|
+
- `LOG_FORMAT` — `google-cloud-logging` (default) or `victoria-logs`
|
|
90
90
|
- `LOG_LEVEL` — `debug`, `info` (default), `warn`, `error`, `fatal`
|
|
91
91
|
|
|
92
92
|
## Child Loggers
|
|
@@ -103,7 +103,7 @@ requestLogger.info('Processing request')
|
|
|
103
103
|
|----------|---------|-------------|
|
|
104
104
|
| `JSON_LOGS` | `false` | Enable JSON output (`true` for production) |
|
|
105
105
|
| `LOG_LEVEL` | `info`/`debug` | Minimum log level (defaults to `info` when JSON, `debug` otherwise) |
|
|
106
|
-
| `LOG_FORMAT` | `
|
|
106
|
+
| `LOG_FORMAT` | `google-cloud-logging` | Formatter for preload module (`google-cloud-logging` or `victoria-logs`) |
|
|
107
107
|
|
|
108
108
|
## Runtime Detection
|
|
109
109
|
|
|
@@ -117,8 +117,8 @@ The logger auto-detects the runtime and uses the fastest available I/O:
|
|
|
117
117
|
| Subpath | Export |
|
|
118
118
|
|---------|--------|
|
|
119
119
|
| `jsonl-logger` | `Logger`, `logger`, types |
|
|
120
|
-
| `jsonl-logger/google-cloud` | `
|
|
121
|
-
| `jsonl-logger/victoria-logs` | `
|
|
120
|
+
| `jsonl-logger/google-cloud-logging` | `GoogleCloudLogging` formatter |
|
|
121
|
+
| `jsonl-logger/victoria-logs` | `VictoriaLogs` formatter |
|
|
122
122
|
| `jsonl-logger/intercept` | `intercept()`, `originalConsole` |
|
|
123
123
|
| `jsonl-logger/preload` | Side-effect auto-intercept |
|
|
124
124
|
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{g as a}from"./index-c56gnbme.js";export{a as GoogleCloudLogging};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{d as I,e as U,f as H}from"./index-abhqk8y0.js";import{g as T}from"./index-c56gnbme.js";var G={log:console.log.bind(console),info:console.info.bind(console),warn:console.warn.bind(console),error:console.error.bind(console),debug:console.debug.bind(console)};function Y(...j){let b="";for(let k=0;k<j.length;k++){if(k>0)b+=" ";let q=j[k];if(typeof q==="string")b+=U(q);else if(q instanceof Error)b+=q.message;else try{b+=JSON.stringify(q)}catch{b+=String(q)}}return b.trim()}function Z(...j){let b;for(let k of j)if(typeof k==="object"&&k!==null&&!(k instanceof Error)&&!Array.isArray(k)){if(!b)b={};Object.assign(b,k)}return b}function _(...j){for(let b of j)if(b instanceof Error)return b}var Q={debug:!1,info:!1,warn:!1,error:!0,fatal:!0};function $(j,b,k,q){let F=`"${b.messageKey}"`;return(...z)=>{if(z.length===1&&typeof z[0]==="string"&&z[0].charCodeAt(0)===123&&z[0].includes(F)){H(z[0],Q[j]);return}if(I[j]<k)return;let B=Y(...z);if(q&&!q(j,B))return;let W=Z(...z),D=_(...z),X={level:j,message:B,timestamp:new Date().toISOString(),context:W||{},error:D?{name:D.name,message:D.message,stack:D.stack}:void 0};H(JSON.stringify(b.format(X)),Q[j])}}var R="__jsonlLoggerIntercepted";function J(j){if(globalThis[R])return;globalThis[R]=!0;let b=j?.formatter??T,k=I[j?.level??"debug"],q=j?.filter,F=[["log","info"],["info","info"],["warn","warn"],["error","error"],["debug","debug"]];for(let[z,B]of F)console[z]=$(B,b,k,q)}
|
|
2
|
+
export{G as b,J as c};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
var t={debug:"DEBUG",info:"INFO",warn:"WARNING",error:"ERROR",fatal:"CRITICAL"},o={messageKey:"message",format(e){let r={message:e.message,timestamp:e.timestamp,severity:t[e.level],...e.context};if(e.error){if(r["error.name"]=e.error.name,r["error.message"]=e.error.message,e.error.stack)r["error.stack"]=e.error.stack}return r}};
|
|
2
|
-
export{o as
|
|
2
|
+
export{o as g};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
var t={messageKey:"_msg",format(e){let r={_msg:e.message,_time:e.timestamp,level:e.level,...e.context};if(e.error){if(r["error.name"]=e.error.name,r["error.message"]=e.error.message,e.error.stack)r["error.stack"]=e.error.stack}return r}};
|
|
2
|
-
export{t as
|
|
2
|
+
export{t as a};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { LogContext, LoggerOptions } from './types';
|
|
2
|
-
export type { Formatter, InterceptOptions, LogContext, LoggerOptions, LogLevel, LogRecord, } from './types';
|
|
2
|
+
export type { Formatter, FormatterName, InterceptOptions, LogContext, LoggerOptions, LogLevel, LogRecord, } from './types';
|
|
3
3
|
export { logLevelValues, stripAnsi } from './types';
|
|
4
4
|
export declare class Logger {
|
|
5
5
|
private ctx;
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{d as n,e as
|
|
1
|
+
import{d as n,e as g,f as m}from"./index-abhqk8y0.js";import{g as c}from"./index-c56gnbme.js";var L=process.env.JSON_LOGS==="true",v=process.env.LOG_LEVEL||(L?"info":"debug"),h={debug:!1,info:!1,warn:!1,error:!0,fatal:!0};class a{ctx;min;json;fmt;constructor(o,t){this.ctx=o||{},this.json=t?.json??L,this.fmt=t?.formatter??c;let e=t?.level??v;this.min=n[e]??n.info}child(o){let t=new a({...this.ctx,...o},{json:this.json,formatter:this.fmt});return t.min=this.min,t}log(o,t,e,r){if(n[o]<this.min)return;let s={level:o,message:this.json?g(t).trim():t,timestamp:new Date().toISOString(),context:e?{...this.ctx,...e}:this.ctx};if(r)s.error={name:r.name,message:r.message,stack:r.stack};if(this.json)m(JSON.stringify(this.fmt.format(s)),h[o]);else this.logPlain(o,s)}logPlain(o,t){let e={debug:"\x1B[36m",info:"\x1B[32m",warn:"\x1B[33m",error:"\x1B[31m",fatal:"\x1B[35m"},r="\x1B[0m",s=e[o],f=new Date(t.timestamp).toLocaleTimeString("en-US",{hour12:!1}),p=o.toUpperCase().padEnd(5),l=t.context,x=Object.keys(l).length>0?` ${JSON.stringify(l)}`:"",d=t.error?` [${t.error.name}: ${t.error.message}]`:"",i=`${s}${f} ${p}\x1B[0m ${t.message}${x}${d}`;switch(o){case"debug":console.debug(i);break;case"warn":console.warn(i);break;case"error":case"fatal":console.error(i);break;default:console.log(i)}}debug(o,t){this.log("debug",o,t)}info(o,t){this.log("info",o,t)}warn(o,t){this.log("warn",o,t)}error(o,t,e){this.log("error",o,t,e)}fatal(o,t,e){this.log("fatal",o,t,e)}}var C=new a;export{g as stripAnsi,C as logger,n as logLevelValues,a as Logger};
|
package/dist/intercept.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{b as a,c as b}from"./index-
|
|
1
|
+
import{b as a,c as b}from"./index-5vvjt922.js";import"./index-abhqk8y0.js";import"./index-c56gnbme.js";export{a as originalConsole,b as intercept};
|
package/dist/preload.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{a as r}from"./index-
|
|
1
|
+
import{a as r}from"./index-jvj9ywjg.js";import{c as t}from"./index-5vvjt922.js";import"./index-abhqk8y0.js";import{g as o}from"./index-c56gnbme.js";var e={"google-cloud-logging":o,"victoria-logs":r},m=process.env.LOG_FORMAT||"google-cloud-logging",a=e[m]??o,g=process.env.LOG_LEVEL||"info";t({formatter:a,level:g});
|
package/dist/types.d.ts
CHANGED
|
@@ -20,6 +20,7 @@ export type LoggerOptions = {
|
|
|
20
20
|
json?: boolean;
|
|
21
21
|
level?: LogLevel;
|
|
22
22
|
};
|
|
23
|
+
export type FormatterName = 'google-cloud-logging' | 'victoria-logs';
|
|
23
24
|
export type InterceptOptions = {
|
|
24
25
|
formatter?: Formatter;
|
|
25
26
|
filter?: (level: LogLevel, message: string) => boolean;
|
package/dist/victoria-logs.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { Formatter } from './types';
|
|
2
|
-
export declare const
|
|
2
|
+
export declare const VictoriaLogs: Formatter;
|
package/dist/victoria-logs.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{a}from"./index-jvj9ywjg.js";export{a as VictoriaLogs};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jsonl-logger",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "Lightweight ESM-only JSON Lines logger with pluggable formatters for Google Cloud Logging, VictoriaLogs, and more",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -14,10 +14,10 @@
|
|
|
14
14
|
"import": "./dist/index.js",
|
|
15
15
|
"types": "./dist/index.d.ts"
|
|
16
16
|
},
|
|
17
|
-
"./google-cloud": {
|
|
18
|
-
"bun": "./src/google-cloud.ts",
|
|
19
|
-
"import": "./dist/google-cloud.js",
|
|
20
|
-
"types": "./dist/google-cloud.d.ts"
|
|
17
|
+
"./google-cloud-logging": {
|
|
18
|
+
"bun": "./src/google-cloud-logging.ts",
|
|
19
|
+
"import": "./dist/google-cloud-logging.js",
|
|
20
|
+
"types": "./dist/google-cloud-logging.d.ts"
|
|
21
21
|
},
|
|
22
22
|
"./victoria-logs": {
|
|
23
23
|
"bun": "./src/victoria-logs.ts",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
],
|
|
42
42
|
"scripts": {
|
|
43
43
|
"build": "bun run build:js && bun run build:types",
|
|
44
|
-
"build:js": "bun build src/index.ts src/google-cloud.ts src/victoria-logs.ts src/intercept.ts src/preload.ts --outdir dist --target node --format esm --splitting --minify",
|
|
44
|
+
"build:js": "bun build src/index.ts src/google-cloud-logging.ts src/victoria-logs.ts src/intercept.ts src/preload.ts --outdir dist --target node --format esm --splitting --minify",
|
|
45
45
|
"build:types": "tsc --emitDeclarationOnly",
|
|
46
46
|
"lint": "biome check --write .",
|
|
47
47
|
"test": "bun test"
|
|
@@ -8,7 +8,7 @@ const severityMap: Record<LogLevel, string> = {
|
|
|
8
8
|
fatal: 'CRITICAL',
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
export const
|
|
11
|
+
export const GoogleCloudLogging: Formatter = {
|
|
12
12
|
messageKey: 'message',
|
|
13
13
|
format(record: LogRecord): Record<string, unknown> {
|
|
14
14
|
const entry: Record<string, unknown> = {
|
package/src/index.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { GoogleCloudLogging } from './google-cloud-logging'
|
|
1
2
|
import type {
|
|
2
3
|
Formatter,
|
|
3
4
|
LogContext,
|
|
@@ -6,10 +7,10 @@ import type {
|
|
|
6
7
|
LogRecord,
|
|
7
8
|
} from './types'
|
|
8
9
|
import { logLevelValues, stripAnsi, write } from './types'
|
|
9
|
-
import { victoriaLogs } from './victoria-logs'
|
|
10
10
|
|
|
11
11
|
export type {
|
|
12
12
|
Formatter,
|
|
13
|
+
FormatterName,
|
|
13
14
|
InterceptOptions,
|
|
14
15
|
LogContext,
|
|
15
16
|
LoggerOptions,
|
|
@@ -40,7 +41,7 @@ export class Logger {
|
|
|
40
41
|
constructor(context?: LogContext, options?: LoggerOptions) {
|
|
41
42
|
this.ctx = context || {}
|
|
42
43
|
this.json = options?.json ?? defaultJson
|
|
43
|
-
this.fmt = options?.formatter ??
|
|
44
|
+
this.fmt = options?.formatter ?? GoogleCloudLogging
|
|
44
45
|
const level: LogLevel = options?.level ?? defaultLevel
|
|
45
46
|
this.min = logLevelValues[level] ?? logLevelValues.info
|
|
46
47
|
}
|
package/src/intercept.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
import { GoogleCloudLogging } from './google-cloud-logging'
|
|
1
2
|
import type { Formatter, InterceptOptions, LogLevel } from './types'
|
|
2
3
|
import { logLevelValues, stripAnsi, write } from './types'
|
|
3
|
-
import { victoriaLogs } from './victoria-logs'
|
|
4
4
|
|
|
5
5
|
type ConsoleMethods = {
|
|
6
6
|
log: typeof console.log
|
|
@@ -118,7 +118,7 @@ export function intercept(options?: InterceptOptions): void {
|
|
|
118
118
|
if ((globalThis as Record<string, unknown>)[guardKey]) return
|
|
119
119
|
;(globalThis as Record<string, unknown>)[guardKey] = true
|
|
120
120
|
|
|
121
|
-
const formatter = options?.formatter ??
|
|
121
|
+
const formatter = options?.formatter ?? GoogleCloudLogging
|
|
122
122
|
const minLevel = logLevelValues[options?.level ?? 'debug']
|
|
123
123
|
const filter = options?.filter
|
|
124
124
|
|
package/src/preload.ts
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { GoogleCloudLogging } from './google-cloud-logging'
|
|
2
2
|
import { intercept } from './intercept'
|
|
3
|
-
import type { Formatter } from './types'
|
|
4
|
-
import {
|
|
3
|
+
import type { Formatter, FormatterName } from './types'
|
|
4
|
+
import { VictoriaLogs } from './victoria-logs'
|
|
5
5
|
|
|
6
|
-
const formatters: Record<
|
|
7
|
-
'
|
|
8
|
-
'
|
|
6
|
+
const formatters: Record<FormatterName, Formatter> = {
|
|
7
|
+
'google-cloud-logging': GoogleCloudLogging,
|
|
8
|
+
'victoria-logs': VictoriaLogs,
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
const format = process.env.LOG_FORMAT ||
|
|
12
|
-
|
|
11
|
+
const format = (process.env.LOG_FORMAT ||
|
|
12
|
+
'google-cloud-logging') as FormatterName
|
|
13
|
+
const formatter = formatters[format] ?? GoogleCloudLogging
|
|
13
14
|
const level =
|
|
14
15
|
(process.env.LOG_LEVEL as 'debug' | 'info' | 'warn' | 'error' | 'fatal') ||
|
|
15
16
|
'info'
|
package/src/types.ts
CHANGED
package/src/victoria-logs.ts
CHANGED
package/dist/google-cloud.d.ts
DELETED
package/dist/google-cloud.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{a}from"./index-rhgqp0zd.js";export{a as googleCloud};
|
package/dist/index-x1ss6ry6.js
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
import{d as G,e as Q,f as F}from"./index-abhqk8y0.js";import{g as R}from"./index-rxqh9v58.js";var A={log:console.log.bind(console),info:console.info.bind(console),warn:console.warn.bind(console),error:console.error.bind(console),debug:console.debug.bind(console)};function W(...j){let b="";for(let k=0;k<j.length;k++){if(k>0)b+=" ";let q=j[k];if(typeof q==="string")b+=Q(q);else if(q instanceof Error)b+=q.message;else try{b+=JSON.stringify(q)}catch{b+=String(q)}}return b.trim()}function X(...j){let b;for(let k of j)if(typeof k==="object"&&k!==null&&!(k instanceof Error)&&!Array.isArray(k)){if(!b)b={};Object.assign(b,k)}return b}function Y(...j){for(let b of j)if(b instanceof Error)return b}var H={debug:!1,info:!1,warn:!1,error:!0,fatal:!0};function Z(j,b,k,q){let D=`"${b.messageKey}"`;return(...z)=>{if(z.length===1&&typeof z[0]==="string"&&z[0].charCodeAt(0)===123&&z[0].includes(D)){F(z[0],H[j]);return}if(G[j]<k)return;let B=W(...z);if(q&&!q(j,B))return;let T=X(...z),C=Y(...z),U={level:j,message:B,timestamp:new Date().toISOString(),context:T||{},error:C?{name:C.name,message:C.message,stack:C.stack}:void 0};F(JSON.stringify(b.format(U)),H[j])}}var I="__jsonlLoggerIntercepted";function J(j){if(globalThis[I])return;globalThis[I]=!0;let b=j?.formatter??R,k=G[j?.level??"debug"],q=j?.filter,D=[["log","info"],["info","info"],["warn","warn"],["error","error"],["debug","debug"]];for(let[z,B]of D)console[z]=Z(B,b,k,q)}
|
|
2
|
-
export{A as b,J as c};
|