@larvit/log 1.2.2 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2022 Larv IT AB
3
+ Copyright (c) 2024 Larv IT AB
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -11,7 +11,7 @@ Zero dependency, structured logging with a simple interface.
11
11
  ```javascript
12
12
  import { Log } from "@larvit/log";
13
13
 
14
- const log = new Log();
14
+ const log = new Log("silly"); // Replace "silly" with the minimum log level you want. Defaults to "info"
15
15
  log.error("Apocalypse! :O"); // stderr
16
16
  log.warn("The chaos is near"); // stderr
17
17
  log.info("All is well, but this message is important"); // stdout
@@ -20,14 +20,48 @@ log.debug("A lot of detailed logs to debug your application"); // stdout
20
20
  log.silly("Open the flood gates!"); // stdout
21
21
  ```
22
22
 
23
+ To clone your log instance: `const log2 = log.clone();`.
24
+
25
+ ### Group your logs
26
+
27
+ To get tracing, timings, spans etc you can group your logs like this example:
28
+
29
+ ```javascript
30
+ import { Log } from "@larvit/log";
31
+
32
+ // Creates an outer log context
33
+ const appLog = new Log();
34
+
35
+ // Just an example on a request/response http handler that you want to log
36
+ function myRequsetHandler(req, res) {
37
+ // Creates an inner log context for this specific request
38
+ const reqLog = new Log({
39
+ context: { requestId: crypto.randomUUID() },
40
+ parentLog: appLog,
41
+ spanName: "request",
42
+ });
43
+
44
+ reqLog.info("Incoming request", { url: req.url });
45
+
46
+ // ... Here be loads of request handler logic ...
47
+
48
+ // Explicitly tell that this inner log is now ended.
49
+ // This is used to set timings etc.
50
+ reqLog.end();
51
+ }
52
+
53
+ ```
54
+
23
55
  ### Configuration
24
56
 
25
- **Log level**
57
+ **Log level only**
26
58
  `const log = new Log("info");` Will only output error, warn and info logs. This is the default. All possible options: "error", "warn", "info", "verbose", "debug", "silly" and "none".
27
59
 
28
- **Other options**
60
+ **All options**
29
61
  ```javascript
30
62
  const log = new Log({
63
+ // All options is optional
64
+
31
65
  // Context will be appended as metadata to all log entries
32
66
  // Default is an empty context
33
67
  context: {
@@ -38,14 +72,43 @@ const log = new Log({
38
72
  // Options are "text" and "json", "text" is the default
39
73
  format: "text",
40
74
 
41
- // Default to "info", same as Log level section above
75
+ // Defaults to "info", same as Log level only section above
42
76
  logLevel: "info",
43
77
 
44
- // The function that formats the log entry
78
+ // The function that formats the log entry, default is shown here
45
79
  entryFormatter: ({ logLevel, metadata, msg }) => {
46
80
  return `${logLevel}: ${msg} ${JSON.stringify(metadata)}`;
47
81
  },
48
82
 
83
+ // Open Telemetry additional http headers
84
+ // For example:
85
+ // { Authorization: "Bearer xxx" }
86
+ // Defaults to null
87
+ // Added in 1.4.0
88
+ otlpAdditionalHeaders: null,
89
+
90
+ // Open Telemetry http endpoint to send spans, traces and logs to.
91
+ // For example http://127.0.0.1:4318
92
+ // Defaults to null
93
+ // Added in 1.3.0
94
+ otlpHttpBaseURI: null,
95
+
96
+ // Group logs together under a specific parent
97
+ // Used for spans and traces in Open Telemetry etc.
98
+ // Defaults to null, creating no span in otlp
99
+ // Added in 1.3.0
100
+ parentLog: new Log(),
101
+
102
+ // If set to true, append spanName, spanId and traceId to the context output
103
+ // Defaults to false
104
+ // Added in 1.3.0
105
+ printTraceInfo: false,
106
+
107
+ // Use a specific span name. Any log using this log as a parent will be
108
+ // grouped under this span name.
109
+ // Defaults to be the same as the span id, that is internally generated for each span
110
+ spanName: "my-span",
111
+
49
112
  // Function that will be called to write log levels silly, debug, verbose and info.
50
113
  // Defaults to console.log
51
114
  stdout: console.log,
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@larvit/log",
3
- "version": "1.2.2",
3
+ "version": "1.4.0",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
- "packageManager": "yarn@3.6.3",
6
+ "packageManager": "yarn@4.5.1",
7
7
  "scripts": {
8
8
  "build": "rm -f index.js index.d.ts index.js.map && tsc && uglifyjs index.js -o index.js",
9
9
  "lint-fix": "eslint --fix index.ts test.ts",
@@ -13,14 +13,16 @@
13
13
  },
14
14
  "devDependencies": {
15
15
  "@larvit/eslint-config-typescript-esm": "1.2.1",
16
- "@randomgoods/tap-spec": "5.0.3",
17
- "@types/node": "20.5.7",
18
- "@types/tape": "5.6.0",
19
- "eslint": "8.48.0",
20
- "tape": "5.6.6",
21
- "ts-node": "10.9.1",
22
- "typescript": "5.2.2",
23
- "uglify-js": "3.17.4"
16
+ "@randomgoods/tap-spec": "5.0.4",
17
+ "@types/express": "5.0.0",
18
+ "@types/node": "22.9.1",
19
+ "@types/tape": "5.6.4",
20
+ "eslint": "8.57.1",
21
+ "express": "5.0.0",
22
+ "tape": "5.9.0",
23
+ "ts-node": "10.9.2",
24
+ "typescript": "5.6.3",
25
+ "uglify-js": "3.19.3"
24
26
  },
25
27
  "publishConfig": {
26
28
  "access": "public",
package/index.d.ts DELETED
@@ -1,40 +0,0 @@
1
- export type Metadata = {
2
- [key: string]: string;
3
- };
4
- export type LogShorthand = (msg: string, metadata?: Metadata) => void;
5
- export interface LogInt {
6
- error: LogShorthand;
7
- warn: LogShorthand;
8
- info: LogShorthand;
9
- verbose: LogShorthand;
10
- debug: LogShorthand;
11
- silly: LogShorthand;
12
- }
13
- export type LogLevel = keyof LogInt;
14
- export type EntryFormatterConf = {
15
- logLevel: LogLevel;
16
- metadata?: Metadata;
17
- msg: string;
18
- };
19
- export type LogConf = {
20
- context?: Metadata;
21
- entryFormatter?: (conf: EntryFormatterConf) => string;
22
- format?: "text" | "json";
23
- logLevel?: LogLevel | "none";
24
- stderr?: (msg: string) => void;
25
- stdout?: (msg: string) => void;
26
- };
27
- export declare function msgJsonFormatter(conf: EntryFormatterConf): string;
28
- export declare function msgTextFormatter(conf: EntryFormatterConf): string;
29
- export declare class Log implements LogInt {
30
- #private;
31
- context: Metadata;
32
- constructor(conf?: LogConf | LogLevel | "none");
33
- clone(conf?: LogConf | LogLevel | "none"): Log;
34
- error(msg: string, metadata?: Metadata): void;
35
- warn(msg: string, metadata?: Metadata): void;
36
- info(msg: string, metadata?: Metadata): void;
37
- verbose(msg: string, metadata?: Metadata): void;
38
- debug(msg: string, metadata?: Metadata): void;
39
- silly(msg: string, metadata?: Metadata): void;
40
- }
package/index.js DELETED
@@ -1 +0,0 @@
1
- export function msgJsonFormatter(conf){const payload=Object.assign(conf.metadata,{logLevel:conf.logLevel,msg:conf.msg,time:(new Date).toISOString()});return JSON.stringify(payload)}export function msgTextFormatter(conf){let levelOut="";if(conf.logLevel==="silly"){levelOut="sil"}else if(conf.logLevel==="debug"){levelOut="deb"}else if(conf.logLevel==="verbose"){levelOut="ver"}else if(conf.logLevel==="info"){levelOut="inf"}else if(conf.logLevel==="warn"){levelOut="war"}else if(conf.logLevel==="error"){levelOut="err"}else{throw new Error(`Invalid conf.logLevel: "${conf.logLevel}"`)}let str=`${(new Date).toISOString().substring(0,19)}Z [${levelOut}] ${conf.msg}`;const metadataStr=JSON.stringify(conf.metadata);if(metadataStr!=="{}"){str+=` ${JSON.stringify(conf.metadata)}`}return str}export class Log{context;#conf;#logLevel;#entryFormatter;#stderr;#stdout;constructor(conf){if(conf===undefined){conf={}}else if(typeof conf==="string"){conf={logLevel:conf}}if(conf.logLevel===undefined){conf.logLevel="info"}if(conf.entryFormatter===undefined&&conf.format==="json"){conf.entryFormatter=msgJsonFormatter}else if(conf.entryFormatter===undefined){conf.entryFormatter=msgTextFormatter}if(conf.stderr===undefined){conf.stderr=console.error}if(conf.stdout===undefined){conf.stdout=console.log}this.#conf=conf;this.#logLevel=conf.logLevel;this.#entryFormatter=conf.entryFormatter;this.#stderr=conf.stderr;this.#stdout=conf.stdout;this.context=conf.context||{}}clone(conf){if(conf===undefined){conf={}}else if(typeof conf==="string"){conf={logLevel:conf}}if(conf.logLevel===undefined){conf.logLevel=this.#logLevel}if(this.#conf.format!=="json"&&conf.format==="json"){conf.entryFormatter=msgJsonFormatter}else{conf.entryFormatter=this.#entryFormatter}if(conf.stderr===undefined){conf.stderr=this.#conf.stderr}if(conf.stdout===undefined){conf.stdout=this.#conf.stdout}conf.context={...this.context,...conf.context};return new Log(conf)}error(msg,metadata){if(this.#logLevel==="none")return;this.#stderr(this.#entryFormatter({logLevel:"error",metadata:Object.assign(metadata||{},this.context),msg:msg}))}warn(msg,metadata){if(["none","error"].includes(this.#logLevel))return;this.#stderr(this.#entryFormatter({logLevel:"warn",metadata:Object.assign(metadata||{},this.context),msg:msg}))}info(msg,metadata){if(["none","error","warn"].includes(this.#logLevel))return;this.#stdout(this.#entryFormatter({logLevel:"info",metadata:Object.assign(metadata||{},this.context),msg:msg}))}verbose(msg,metadata){if(["none","error","warn","info"].includes(this.#logLevel))return;this.#stdout(this.#entryFormatter({logLevel:"verbose",metadata:Object.assign(metadata||{},this.context),msg:msg}))}debug(msg,metadata){if(["none","error","warn","info","verbose"].includes(this.#logLevel))return;this.#stdout(this.#entryFormatter({logLevel:"debug",metadata:Object.assign(metadata||{},this.context),msg:msg}))}silly(msg,metadata){if(["none","error","warn","info","verbose","debug"].includes(this.#logLevel))return;this.#stdout(this.#entryFormatter({logLevel:"silly",metadata:Object.assign(metadata||{},this.context),msg:msg}))}}
package/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAkCA,MAAM,UAAU,gBAAgB,CAAC,IAAwB;IACxD,MAAM,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,QAAQ,EAAE;QAC5C,QAAQ,EAAE,IAAI,CAAC,QAAQ;QACvB,GAAG,EAAE,IAAI,CAAC,GAAG;QACb,IAAI,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;KAC9B,CAAC,CAAC;IAEH,OAAO,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;AAChC,CAAC;AAED,MAAM,UAAU,gBAAgB,CAAC,IAAwB;IACxD,IAAI,QAAQ,GAAG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