jsonl-logger 0.1.1 → 0.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.
- package/README.md +21 -19
- package/dist/google-cloud-logging.d.ts +2 -0
- package/dist/{index-rhgqp0zd.js → google-cloud-logging.js} +1 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.js +3 -1
- package/dist/intercept.js +3 -1
- package/dist/preload.js +3 -1
- package/dist/types.d.ts +3 -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 +20 -4
- package/src/intercept.ts +2 -2
- package/src/preload.ts +15 -13
- package/src/types.ts +5 -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-abhqk8y0.js +0 -4
- package/dist/index-rxqh9v58.js +0 -2
- package/dist/index-x1ss6ry6.js +0 -2
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# jsonl-logger
|
|
2
2
|
|
|
3
|
-
Lightweight JSON Lines logger with pluggable formatters. Modern ESM-only, zero dependencies, Bun-first, works on Node.js and Deno.
|
|
3
|
+
Lightweight JSON Lines (JSONL) logger with pluggable formatters. Modern ESM-only, zero dependencies, Bun-first, works on Node.js and Deno.
|
|
4
4
|
|
|
5
5
|
## Install
|
|
6
6
|
|
|
@@ -19,24 +19,29 @@ logger.info('Server started', { port: 3000 })
|
|
|
19
19
|
logger.error('Request failed', { path: '/api' }, new Error('timeout'))
|
|
20
20
|
```
|
|
21
21
|
|
|
22
|
+
Without `LOG_FORMAT`, the logger outputs colored plain text — ideal for local development. Set `LOG_FORMAT` to enable structured JSON for production (see Formatters below).
|
|
23
|
+
|
|
22
24
|
## Formatters
|
|
23
25
|
|
|
24
|
-
|
|
26
|
+
Set `LOG_FORMAT` to enable JSON output with a specific formatter:
|
|
25
27
|
|
|
26
28
|
### Google Cloud Logging
|
|
27
29
|
|
|
28
|
-
```
|
|
29
|
-
|
|
30
|
-
|
|
30
|
+
```bash
|
|
31
|
+
LOG_FORMAT=google-cloud-logging bun run server.ts
|
|
32
|
+
```
|
|
31
33
|
|
|
32
|
-
|
|
34
|
+
```typescript
|
|
33
35
|
// Output: {"message":"...","timestamp":"...","severity":"INFO",...}
|
|
34
36
|
```
|
|
35
37
|
|
|
36
|
-
### VictoriaLogs
|
|
38
|
+
### VictoriaLogs
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
LOG_FORMAT=victoria-logs bun run server.ts
|
|
42
|
+
```
|
|
37
43
|
|
|
38
44
|
```typescript
|
|
39
|
-
import { victoriaLogs } from 'jsonl-logger/victoria-logs'
|
|
40
45
|
// Output: {"_msg":"...","_time":"...","level":"info",...}
|
|
41
46
|
```
|
|
42
47
|
|
|
@@ -64,8 +69,8 @@ Monkey-patch `console.*` methods to output structured JSON — captures logs fro
|
|
|
64
69
|
import { intercept, originalConsole } from 'jsonl-logger/intercept'
|
|
65
70
|
|
|
66
71
|
intercept({
|
|
67
|
-
// Optional: custom formatter (default:
|
|
68
|
-
formatter:
|
|
72
|
+
// Optional: custom formatter (default: GoogleCloudLogging)
|
|
73
|
+
formatter: VictoriaLogs,
|
|
69
74
|
// Optional: filter out noisy messages
|
|
70
75
|
filter: (level, message) => !message.includes('deprecation'),
|
|
71
76
|
// Optional: minimum log level
|
|
@@ -82,12 +87,10 @@ originalConsole.log('bypass interception')
|
|
|
82
87
|
Auto-intercept from first line using `--preload`:
|
|
83
88
|
|
|
84
89
|
```bash
|
|
85
|
-
bun --preload jsonl-logger/preload server.js
|
|
90
|
+
LOG_FORMAT=victoria-logs bun --preload jsonl-logger/preload server.js
|
|
86
91
|
```
|
|
87
92
|
|
|
88
|
-
|
|
89
|
-
- `LOG_FORMAT` — `google-cloud` or `victoria-logs` (default)
|
|
90
|
-
- `LOG_LEVEL` — `debug`, `info` (default), `warn`, `error`, `fatal`
|
|
93
|
+
The preload module reads `LOG_FORMAT` and only activates when it's set. Safe to include unconditionally — it's a no-op without `LOG_FORMAT`.
|
|
91
94
|
|
|
92
95
|
## Child Loggers
|
|
93
96
|
|
|
@@ -101,9 +104,8 @@ requestLogger.info('Processing request')
|
|
|
101
104
|
|
|
102
105
|
| Variable | Default | Description |
|
|
103
106
|
|----------|---------|-------------|
|
|
104
|
-
| `
|
|
105
|
-
| `LOG_LEVEL` | `info`/`debug` | Minimum log level (
|
|
106
|
-
| `LOG_FORMAT` | `victoria-logs` | Formatter for preload module (`google-cloud` or `victoria-logs`) |
|
|
107
|
+
| `LOG_FORMAT` | _(unset)_ | Set to enable JSON: `google-cloud-logging` or `victoria-logs` |
|
|
108
|
+
| `LOG_LEVEL` | `info`/`debug` | Minimum log level (`info` when JSON, `debug` otherwise) |
|
|
107
109
|
|
|
108
110
|
## Runtime Detection
|
|
109
111
|
|
|
@@ -117,8 +119,8 @@ The logger auto-detects the runtime and uses the fastest available I/O:
|
|
|
117
119
|
| Subpath | Export |
|
|
118
120
|
|---------|--------|
|
|
119
121
|
| `jsonl-logger` | `Logger`, `logger`, types |
|
|
120
|
-
| `jsonl-logger/google-cloud` | `
|
|
121
|
-
| `jsonl-logger/victoria-logs` | `
|
|
122
|
+
| `jsonl-logger/google-cloud-logging` | `GoogleCloudLogging` formatter |
|
|
123
|
+
| `jsonl-logger/victoria-logs` | `VictoriaLogs` formatter |
|
|
122
124
|
| `jsonl-logger/intercept` | `intercept()`, `originalConsole` |
|
|
123
125
|
| `jsonl-logger/preload` | Side-effect auto-intercept |
|
|
124
126
|
|
|
@@ -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 a};
|
|
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}};export{o as GoogleCloudLogging};
|
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,3 @@
|
|
|
1
|
-
|
|
1
|
+
var C={debug:"DEBUG",info:"INFO",warn:"WARNING",error:"ERROR",fatal:"CRITICAL"},a={messageKey:"message",format(o){let t={message:o.message,timestamp:o.timestamp,severity:C[o.level],...o.context};if(o.error){if(t["error.name"]=o.error.name,t["error.message"]=o.error.message,o.error.stack)t["error.stack"]=o.error.stack}return t}};var f={messageKey:"_msg",format(o){let t={_msg:o.message,_time:o.timestamp,level:o.level,...o.context};if(o.error){if(t["error.name"]=o.error.name,t["error.message"]=o.error.message,o.error.stack)t["error.stack"]=o.error.stack}return t}};var i=process.env.LOG_FORMAT,v=!!i,n={debug:0,info:1,warn:2,error:3,fatal:4},k=/\x1b\[[0-9;]*m/g;function l(o){return o.replace(k,"")}var m=typeof process<"u"&&process.stdout&&typeof process.stdout.write==="function"?"node":typeof Deno<"u"&&Deno.stdout?"deno":"browser",p=m==="deno"?new TextEncoder:null;function x(o,t){if(m==="node")(t?process.stderr??process.stdout:process.stdout).write(`${o}
|
|
2
|
+
`);else if(m==="deno"&&p){let e=p.encode(`${o}
|
|
3
|
+
`);if(t)Deno.stderr.writeSync(e);else Deno.stdout.writeSync(e)}else if(t)console.error(o);else console.log(o)}var y={"google-cloud-logging":a,"victoria-logs":f},F=i&&y[i]||a,u=v,O=process.env.LOG_LEVEL||(u?"info":"debug"),d={debug:!1,info:!1,warn:!1,error:!0,fatal:!0};class L{ctx;min;json;fmt;constructor(o,t){this.ctx=o||{},this.json=t?.json??u,this.fmt=t?.formatter??F;let e=t?.level??O;this.min=n[e]??n.info}child(o){let t=new L({...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?l(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)x(JSON.stringify(this.fmt.format(s)),d[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],h=new Date(t.timestamp).toLocaleTimeString("en-US",{hour12:!1}),R=o.toUpperCase().padEnd(5),c=t.context,w=Object.keys(c).length>0?` ${JSON.stringify(c)}`:"",b=t.error?` [${t.error.name}: ${t.error.message}]`:"",g=`${s}${h} ${R}\x1B[0m ${t.message}${w}${b}`;switch(o){case"debug":console.debug(g);break;case"warn":console.warn(g);break;case"error":case"fatal":console.error(g);break;default:console.log(g)}}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 V=new L;export{l as stripAnsi,V as logger,n as logLevelValues,L as Logger};
|
package/dist/intercept.js
CHANGED
|
@@ -1 +1,3 @@
|
|
|
1
|
-
|
|
1
|
+
var Q={debug:"DEBUG",info:"INFO",warn:"WARNING",error:"ERROR",fatal:"CRITICAL"},W={messageKey:"message",format(R){let k={message:R.message,timestamp:R.timestamp,severity:Q[R.level],...R.context};if(R.error){if(k["error.name"]=R.error.name,k["error.message"]=R.error.message,R.error.stack)k["error.stack"]=R.error.stack}return k}};var X=process.env.LOG_FORMAT,L=!!X,T={debug:0,info:1,warn:2,error:3,fatal:4},Y=/\x1b\[[0-9;]*m/g;function q(R){return R.replace(Y,"")}var B=typeof process<"u"&&process.stdout&&typeof process.stdout.write==="function"?"node":typeof Deno<"u"&&Deno.stdout?"deno":"browser",j=B==="deno"?new TextEncoder:null;function U(R,k){if(B==="node")(k?process.stderr??process.stdout:process.stdout).write(`${R}
|
|
2
|
+
`);else if(B==="deno"&&j){let I=j.encode(`${R}
|
|
3
|
+
`);if(k)Deno.stderr.writeSync(I);else Deno.stdout.writeSync(I)}else if(k)console.error(R);else console.log(R)}var S={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 Z(...R){let k="";for(let I=0;I<R.length;I++){if(I>0)k+=" ";let C=R[I];if(typeof C==="string")k+=q(C);else if(C instanceof Error)k+=C.message;else try{k+=JSON.stringify(C)}catch{k+=String(C)}}return k.trim()}function _(...R){let k;for(let I of R)if(typeof I==="object"&&I!==null&&!(I instanceof Error)&&!Array.isArray(I)){if(!k)k={};Object.assign(k,I)}return k}function $(...R){for(let k of R)if(k instanceof Error)return k}var w={debug:!1,info:!1,warn:!1,error:!0,fatal:!0};function y(R,k,I,C){let A=`"${k.messageKey}"`;return(...F)=>{if(F.length===1&&typeof F[0]==="string"&&F[0].charCodeAt(0)===123&&F[0].includes(A)){U(F[0],w[R]);return}if(T[R]<I)return;let G=Z(...F);if(C&&!C(R,G))return;let H=_(...F),N=$(...F),O={level:R,message:G,timestamp:new Date().toISOString(),context:H||{},error:N?{name:N.name,message:N.message,stack:N.stack}:void 0};U(JSON.stringify(k.format(O)),w[R])}}var z="__jsonlLoggerIntercepted";function V(R){if(globalThis[z])return;globalThis[z]=!0;let k=R?.formatter??W,I=T[R?.level??"debug"],C=R?.filter,A=[["log","info"],["info","info"],["warn","warn"],["error","error"],["debug","debug"]];for(let[F,G]of A)console[F]=y(G,k,I,C)}export{S as originalConsole,V as intercept};
|
package/dist/preload.js
CHANGED
|
@@ -1 +1,3 @@
|
|
|
1
|
-
|
|
1
|
+
var V={debug:"DEBUG",info:"INFO",warn:"WARNING",error:"ERROR",fatal:"CRITICAL"},F={messageKey:"message",format(m){let n={message:m.message,timestamp:m.timestamp,severity:V[m.level],...m.context};if(m.error){if(n["error.name"]=m.error.name,n["error.message"]=m.error.message,m.error.stack)n["error.stack"]=m.error.stack}return n}};var _={messageKey:"_msg",format(m){let n={_msg:m.message,_time:m.timestamp,level:m.level,...m.context};if(m.error){if(n["error.name"]=m.error.name,n["error.message"]=m.error.message,m.error.stack)n["error.stack"]=m.error.stack}return n}};var p=process.env.LOG_FORMAT,Q=!!p,I={debug:0,info:1,warn:2,error:3,fatal:4},W=/\x1b\[[0-9;]*m/g;function C(m){return m.replace(W,"")}var G=typeof process<"u"&&process.stdout&&typeof process.stdout.write==="function"?"node":typeof Deno<"u"&&Deno.stdout?"deno":"browser",w=G==="deno"?new TextEncoder:null;function N(m,n){if(G==="node")(n?process.stderr??process.stdout:process.stdout).write(`${m}
|
|
2
|
+
`);else if(G==="deno"&&w){let R=w.encode(`${m}
|
|
3
|
+
`);if(n)Deno.stderr.writeSync(R);else Deno.stdout.writeSync(R)}else if(n)console.error(m);else console.log(m)}var $={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 c(...m){let n="";for(let R=0;R<m.length;R++){if(R>0)n+=" ";let o=m[R];if(typeof o==="string")n+=C(o);else if(o instanceof Error)n+=o.message;else try{n+=JSON.stringify(o)}catch{n+=String(o)}}return n.trim()}function j(...m){let n;for(let R of m)if(typeof R==="object"&&R!==null&&!(R instanceof Error)&&!Array.isArray(R)){if(!n)n={};Object.assign(n,R)}return n}function q(...m){for(let n of m)if(n instanceof Error)return n}var A={debug:!1,info:!1,warn:!1,error:!0,fatal:!0};function t(m,n,R,o){let y=`"${n.messageKey}"`;return(...k)=>{if(k.length===1&&typeof k[0]==="string"&&k[0].charCodeAt(0)===123&&k[0].includes(y)){N(k[0],A[m]);return}if(I[m]<R)return;let f=c(...k);if(o&&!o(m,f))return;let T=j(...k),L=q(...k),U={level:m,message:f,timestamp:new Date().toISOString(),context:T||{},error:L?{name:L.name,message:L.message,stack:L.stack}:void 0};N(JSON.stringify(n.format(U)),A[m])}}var B="__jsonlLoggerIntercepted";function O(m){if(globalThis[B])return;globalThis[B]=!0;let n=m?.formatter??F,R=I[m?.level??"debug"],o=m?.filter,y=[["log","info"],["info","info"],["warn","warn"],["error","error"],["debug","debug"]];for(let[k,f]of y)console[k]=t(f,n,R,o)}if(p){let n={"google-cloud-logging":F,"victoria-logs":_}[p]??F,R=process.env.LOG_LEVEL||"info";O({formatter:n,level:R})}
|
package/dist/types.d.ts
CHANGED
|
@@ -20,6 +20,9 @@ export type LoggerOptions = {
|
|
|
20
20
|
json?: boolean;
|
|
21
21
|
level?: LogLevel;
|
|
22
22
|
};
|
|
23
|
+
export type FormatterName = 'google-cloud-logging' | 'victoria-logs';
|
|
24
|
+
export declare const defaultFormat: FormatterName | undefined;
|
|
25
|
+
export declare const isJsonMode: boolean;
|
|
23
26
|
export type InterceptOptions = {
|
|
24
27
|
formatter?: Formatter;
|
|
25
28
|
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
|
-
|
|
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}};export{t as VictoriaLogs};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jsonl-logger",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
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 --
|
|
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 --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,15 +1,24 @@
|
|
|
1
|
+
import { GoogleCloudLogging } from './google-cloud-logging'
|
|
1
2
|
import type {
|
|
2
3
|
Formatter,
|
|
4
|
+
FormatterName,
|
|
3
5
|
LogContext,
|
|
4
6
|
LoggerOptions,
|
|
5
7
|
LogLevel,
|
|
6
8
|
LogRecord,
|
|
7
9
|
} from './types'
|
|
8
|
-
import {
|
|
9
|
-
|
|
10
|
+
import {
|
|
11
|
+
defaultFormat,
|
|
12
|
+
isJsonMode,
|
|
13
|
+
logLevelValues,
|
|
14
|
+
stripAnsi,
|
|
15
|
+
write,
|
|
16
|
+
} from './types'
|
|
17
|
+
import { VictoriaLogs } from './victoria-logs'
|
|
10
18
|
|
|
11
19
|
export type {
|
|
12
20
|
Formatter,
|
|
21
|
+
FormatterName,
|
|
13
22
|
InterceptOptions,
|
|
14
23
|
LogContext,
|
|
15
24
|
LoggerOptions,
|
|
@@ -18,7 +27,14 @@ export type {
|
|
|
18
27
|
} from './types'
|
|
19
28
|
export { logLevelValues, stripAnsi } from './types'
|
|
20
29
|
|
|
21
|
-
const
|
|
30
|
+
const formatters: Record<FormatterName, Formatter> = {
|
|
31
|
+
'google-cloud-logging': GoogleCloudLogging,
|
|
32
|
+
'victoria-logs': VictoriaLogs,
|
|
33
|
+
}
|
|
34
|
+
const defaultFormatter =
|
|
35
|
+
(defaultFormat && formatters[defaultFormat]) || GoogleCloudLogging
|
|
36
|
+
|
|
37
|
+
const defaultJson = isJsonMode
|
|
22
38
|
const defaultLevel: LogLevel =
|
|
23
39
|
(process.env.LOG_LEVEL as LogLevel | undefined) ||
|
|
24
40
|
(defaultJson ? 'info' : 'debug')
|
|
@@ -40,7 +56,7 @@ export class Logger {
|
|
|
40
56
|
constructor(context?: LogContext, options?: LoggerOptions) {
|
|
41
57
|
this.ctx = context || {}
|
|
42
58
|
this.json = options?.json ?? defaultJson
|
|
43
|
-
this.fmt = options?.formatter ??
|
|
59
|
+
this.fmt = options?.formatter ?? defaultFormatter
|
|
44
60
|
const level: LogLevel = options?.level ?? defaultLevel
|
|
45
61
|
this.min = logLevelValues[level] ?? logLevelValues.info
|
|
46
62
|
}
|
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,17 +1,19 @@
|
|
|
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 { defaultFormat } from './types'
|
|
5
|
+
import { VictoriaLogs } from './victoria-logs'
|
|
5
6
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
if (defaultFormat) {
|
|
8
|
+
const formatters: Record<FormatterName, Formatter> = {
|
|
9
|
+
'google-cloud-logging': GoogleCloudLogging,
|
|
10
|
+
'victoria-logs': VictoriaLogs,
|
|
11
|
+
}
|
|
10
12
|
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
'info'
|
|
13
|
+
const formatter = formatters[defaultFormat] ?? GoogleCloudLogging
|
|
14
|
+
const level =
|
|
15
|
+
(process.env.LOG_LEVEL as 'debug' | 'info' | 'warn' | 'error' | 'fatal') ||
|
|
16
|
+
'info'
|
|
16
17
|
|
|
17
|
-
intercept({ formatter, level })
|
|
18
|
+
intercept({ formatter, level })
|
|
19
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -21,6 +21,11 @@ export type LoggerOptions = {
|
|
|
21
21
|
level?: LogLevel
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
+
export type FormatterName = 'google-cloud-logging' | 'victoria-logs'
|
|
25
|
+
|
|
26
|
+
export const defaultFormat = process.env.LOG_FORMAT as FormatterName | undefined
|
|
27
|
+
export const isJsonMode = !!defaultFormat
|
|
28
|
+
|
|
24
29
|
export type InterceptOptions = {
|
|
25
30
|
formatter?: Formatter
|
|
26
31
|
filter?: (level: LogLevel, message: string) => boolean
|
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-abhqk8y0.js
DELETED
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
var B={debug:0,info:1,warn:2,error:3,fatal:4},A=/\x1b\[[0-9;]*m/g;function C(h){return h.replace(A,"")}var w=typeof process<"u"&&process.stdout&&typeof process.stdout.write==="function"?"node":typeof Deno<"u"&&Deno.stdout?"deno":"browser",z=w==="deno"?new TextEncoder:null;function F(h,j){if(w==="node")(j?process.stderr??process.stdout:process.stdout).write(`${h}
|
|
2
|
-
`);else if(w==="deno"&&z){let q=z.encode(`${h}
|
|
3
|
-
`);if(j)Deno.stderr.writeSync(q);else Deno.stdout.writeSync(q)}else if(j)console.error(h);else console.log(h)}
|
|
4
|
-
export{B as d,C as e,F as f};
|
package/dist/index-rxqh9v58.js
DELETED
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};
|