Skip to content

Commit 3631aa1

Browse files
authored
Merge pull request #177 from fullstack-build/development
ESM & CJS + alternation of settings at runtime
2 parents bb0f230 + 6118748 commit 3631aa1

File tree

13 files changed

+183
-49
lines changed

13 files changed

+183
-49
lines changed

README.md

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
🤓 **Stack trace and pretty errors**<br>
3030
👨‍👧‍👦 **Sub logger with inheritance**<br>
3131
🙊 **Mask/hide secrets and keys**<br>
32-
📦 **ESM with tree shaking support**<br>
32+
📦 **CJS & ESM with tree shaking support**<br>
3333
✍️ **Well documented and tested**<br>
3434

3535
## Example
@@ -49,8 +49,6 @@ Donations help me allocate more time for my open source work.
4949

5050
## Install
5151

52-
> **`tslog` is a native ES module.**
53-
5452
```bash
5553
npm install tslog
5654
```
@@ -93,16 +91,26 @@ npm start
9391

9492
**Otherwise:**
9593

96-
Node.js with JavaScript:
94+
ESM: Node.js with JavaScript:
9795
```bash
9896
node --enable-source-maps --experimental-specifier-resolution=node
9997
```
10098

101-
Node.js with TypeScript and `ts-node` (with ESM support):
99+
CJS: Node.js with JavaScript:
100+
```bash
101+
node --enable-source-maps
102+
```
103+
104+
ESM: Node.js with TypeScript and `ts-node`:
102105
```bash
103106
node --enable-source-maps --experimental-specifier-resolution=node --no-warnings --loader ts-node/esm
104107
```
105108

109+
CJS: Node.js with TypeScript and `ts-node`:
110+
```bash
111+
node --enable-source-maps --no-warnings --loader ts-node/cjs
112+
```
113+
106114
Browser:
107115
```html
108116
<!doctype html>
@@ -252,6 +260,30 @@ A `settings` object is the first parameter passed to the `tslog` constructor:
252260
const logger = new Logger<ILogObj>({ /* SETTINGS */ }, defaultLogObject);
253261
```
254262

263+
##### Changing settings at runtime
264+
`settings` is a public property and can also be changed on runtime.
265+
266+
Example on changing `minLevel` on runtime:
267+
268+
```typescript
269+
const logger = new Logger({
270+
minLevel: 1
271+
});
272+
273+
// visible
274+
logger.log(1, "level_one", "LOG1");
275+
// visible
276+
logger.log(2, "level_two", "LOG2");
277+
278+
// change minLevel to 2
279+
logger.settings.minLevel = 2;
280+
281+
// hidden
282+
logger.log(1, "level_one", "LOG3");
283+
// visible
284+
logger.log(2, "level_two", "LOG4");
285+
```
286+
255287
#### Type: pretty, json, hidden
256288

257289
- `pretty` **Default setting** prints out a formatted structured "pretty" log entry.
@@ -298,9 +330,9 @@ secondSubLogger.silly("foo bar 2");
298330

299331
Output:
300332
```bash
301-
2022-11-17 10:45:47.705 SILLY [/examples/nodejs/index2.ts:51] MainLogger foo bar
302-
2022-11-17 10:45:47.706 SILLY [/examples/nodejs/index2.ts:54] MainLogger:FirstSubLogger foo bar 1
303-
2022-11-17 10:45:47.706 SILLY [/examples/nodejs/index2.ts:57] MainLogger:FirstSubLogger:SecondSubLogger foo bar 2
333+
2022-11-17 10:45:47.705 SILLY [/examples/nodejs/index2.ts:51 MainLogger] foo bar
334+
2022-11-17 10:45:47.706 SILLY [/examples/nodejs/index2.ts:54 MainLogger:FirstSubLogger ] foo bar 1
335+
2022-11-17 10:45:47.706 SILLY [/examples/nodejs/index2.ts:57 MainLogger:FirstSubLogger:SecondSubLogger] foo bar 2
304336
```
305337

306338
#### minLevel
@@ -356,6 +388,8 @@ Following settings are available for styling:
356388
- `{{rawIsoStr}}`: Renders the date and time in ISO format (e.g.: YYYY-MM-DDTHH:mm:ss.SSSZ)
357389
- `{{logLevelName}}`: name of the log level
358390
- `{{name}}`: optional name of the current logger and his parents (e.g. "ParentLogger:ThisLogger")
391+
- `{{nameWithDelimiterPrefix}}`: optional name of the current logger (s. above) with a delimiter in the beginning
392+
- `{{nameWithDelimiterSuffix}}`: optional name of the current logger (s. above) with a delimiter at the end
359393
- `{{fullFilePath}}`: a full path starting from `/` root
360394
- `{{filePathWithLine}}`: a full path below the project path with line number
361395
- `prettyErrorTemplate`: template string for error message. Possible placeholders:
@@ -407,6 +441,8 @@ const logger = new Logger({
407441
dateIsoStr: "white",
408442
filePathWithLine: "white",
409443
name: ["white", "bold"],
444+
nameWithDelimiterPrefix: ["white", "bold"],
445+
nameWithDelimiterSuffix: ["white", "bold"],
410446
errorName: ["bold", "bgRedBright", "whiteBright"],
411447
fileName: ["yellow"],
412448
},

build.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
import { build } from "esbuild";
1+
const esbuild = require("esbuild");
22

3-
build({
4-
entryPoints: ["src/index.ts"],
5-
outfile: "dist/browser/index.js",
6-
platform: "browser",
7-
bundle: true,
8-
minify: true,
9-
format: "iife",
10-
globalName: "tslog",
11-
loader: { ".ts": "ts" },
12-
})
3+
esbuild
4+
.build({
5+
entryPoints: ["src/index.ts"],
6+
outfile: "dist/browser/index.js",
7+
platform: "browser",
8+
bundle: true,
9+
minify: true,
10+
format: "iife",
11+
globalName: "tslog",
12+
loader: { ".ts": "ts" },
13+
})
1314
.then(() => console.log("⚡ Done"))
1415
.catch(() => process.exit(1));

docs/README.md

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
🤓 **Stack trace and pretty errors**<br>
3030
👨‍👧‍👦 **Sub logger with inheritance**<br>
3131
🙊 **Mask/hide secrets and keys**<br>
32-
📦 **ESM with tree shaking support**<br>
32+
📦 **CJS & ESM with tree shaking support**<br>
3333
✍️ **Well documented and tested**<br>
3434

3535
## Example
@@ -49,8 +49,6 @@ Donations help me allocate more time for my open source work.
4949

5050
## Install
5151

52-
> **`tslog` is a native ES module.**
53-
5452
```bash
5553
npm install tslog
5654
```
@@ -93,16 +91,26 @@ npm start
9391

9492
**Otherwise:**
9593

96-
Node.js with JavaScript:
94+
ESM: Node.js with JavaScript:
9795
```bash
9896
node --enable-source-maps --experimental-specifier-resolution=node
9997
```
10098

101-
Node.js with TypeScript and `ts-node` (with ESM support):
99+
CJS: Node.js with JavaScript:
100+
```bash
101+
node --enable-source-maps
102+
```
103+
104+
ESM: Node.js with TypeScript and `ts-node`:
102105
```bash
103106
node --enable-source-maps --experimental-specifier-resolution=node --no-warnings --loader ts-node/esm
104107
```
105108

109+
CJS: Node.js with TypeScript and `ts-node`:
110+
```bash
111+
node --enable-source-maps --no-warnings --loader ts-node/cjs
112+
```
113+
106114
Browser:
107115
```html
108116
<!doctype html>
@@ -252,6 +260,30 @@ A `settings` object is the first parameter passed to the `tslog` constructor:
252260
const logger = new Logger<ILogObj>({ /* SETTINGS */ }, defaultLogObject);
253261
```
254262

263+
##### Changing settings at runtime
264+
`settings` is a public property and can also be changed on runtime.
265+
266+
Example on changing `minLevel` on runtime:
267+
268+
```typescript
269+
const logger = new Logger({
270+
minLevel: 1
271+
});
272+
273+
// visible
274+
logger.log(1, "level_one", "LOG1");
275+
// visible
276+
logger.log(2, "level_two", "LOG2");
277+
278+
// change minLevel to 2
279+
logger.settings.minLevel = 2;
280+
281+
// hidden
282+
logger.log(1, "level_one", "LOG3");
283+
// visible
284+
logger.log(2, "level_two", "LOG4");
285+
```
286+
255287
#### Type: pretty, json, hidden
256288

257289
- `pretty` **Default setting** prints out a formatted structured "pretty" log entry.
@@ -298,9 +330,9 @@ secondSubLogger.silly("foo bar 2");
298330

299331
Output:
300332
```bash
301-
2022-11-17 10:45:47.705 SILLY [/examples/nodejs/index2.ts:51] MainLogger foo bar
302-
2022-11-17 10:45:47.706 SILLY [/examples/nodejs/index2.ts:54] MainLogger:FirstSubLogger foo bar 1
303-
2022-11-17 10:45:47.706 SILLY [/examples/nodejs/index2.ts:57] MainLogger:FirstSubLogger:SecondSubLogger foo bar 2
333+
2022-11-17 10:45:47.705 SILLY [/examples/nodejs/index2.ts:51 MainLogger] foo bar
334+
2022-11-17 10:45:47.706 SILLY [/examples/nodejs/index2.ts:54 MainLogger:FirstSubLogger ] foo bar 1
335+
2022-11-17 10:45:47.706 SILLY [/examples/nodejs/index2.ts:57 MainLogger:FirstSubLogger:SecondSubLogger] foo bar 2
304336
```
305337

306338
#### minLevel
@@ -356,6 +388,8 @@ Following settings are available for styling:
356388
- `{{rawIsoStr}}`: Renders the date and time in ISO format (e.g.: YYYY-MM-DDTHH:mm:ss.SSSZ)
357389
- `{{logLevelName}}`: name of the log level
358390
- `{{name}}`: optional name of the current logger and his parents (e.g. "ParentLogger:ThisLogger")
391+
- `{{nameWithDelimiterPrefix}}`: optional name of the current logger (s. above) with a delimiter in the beginning
392+
- `{{nameWithDelimiterSuffix}}`: optional name of the current logger (s. above) with a delimiter at the end
359393
- `{{fullFilePath}}`: a full path starting from `/` root
360394
- `{{filePathWithLine}}`: a full path below the project path with line number
361395
- `prettyErrorTemplate`: template string for error message. Possible placeholders:
@@ -407,6 +441,8 @@ const logger = new Logger({
407441
dateIsoStr: "white",
408442
filePathWithLine: "white",
409443
name: ["white", "bold"],
444+
nameWithDelimiterPrefix: ["white", "bold"],
445+
nameWithDelimiterSuffix: ["white", "bold"],
410446
errorName: ["bold", "bgRedBright", "whiteBright"],
411447
fileName: ["yellow"],
412448
},

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tslog",
3-
"version": "4.1.0",
3+
"version": "4.3.0-0",
44
"description": "📝 Extensible TypeScript Logger for Node.js and Browser: Dependency free, Fully customizable, Pretty errors, stack traces, and JSON output to attachable transports.",
55
"author": "Eugene <[email protected]> (http://fullstack.build/)",
66
"license": "MIT",
@@ -13,18 +13,25 @@
1313
"bugs": {
1414
"url": "https://github.com/fullstack-build/tslog/issues"
1515
},
16-
"main": "./dist/nodejs/index.js",
16+
"main": "./dist/nodejs/cjs/index.js",
17+
"module": "./dist/nodejs/esm/index.js",
18+
"types": "./dist/nodejs/types/index.d.ts",
1719
"browser": {
1820
"tslog": "./dist/browser/index.js",
19-
"util": "",
21+
"util": false,
2022
"./src/runtime/nodejs/index.ts": "./src/runtime/browser/index.ts"
2123
},
22-
"type": "module",
23-
"exports": "./dist/nodejs/index.js",
24+
"exports": {
25+
".": {
26+
"require": "./dist/nodejs/cjs/index.js",
27+
"import": "./dist/nodejs/esm/index.js"
28+
}
29+
},
2430
"scripts": {
25-
"build": "npm run build-server && npm run build-browser",
31+
"build": "npm run build-types && npm run build-server && npm run build-browser",
2632
"build-browser": "node build.js",
27-
"build-server": "tsc -b tsconfig.src.json",
33+
"build-types": "tsc -b tsconfig.types.json",
34+
"build-server": "tsc -b tsconfig.esm.json tsconfig.cjs.json",
2835
"start": "node --enable-source-maps --experimental-specifier-resolution=node examples/dist/examples/nodejs/index2.js",
2936
"dev-ts": "nodemon --watch './**/*.ts' --exec 'node --enable-source-maps --experimental-specifier-resolution=node --no-warnings --loader ts-node/esm' examples/nodejs/index2.ts",
3037
"dev-ts-old-example": "nodemon --watch './**/*.ts' --exec 'node --enable-source-maps --experimental-specifier-resolution=node --no-warnings --loader ts-node/esm' examples/nodejs/index.ts",

src/BaseLogger.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ export * from "./interfaces";
66

77
export class BaseLogger<LogObj> {
88
private readonly runtime: "browser" | "nodejs" | "unknown";
9-
private readonly settings: ISettings<LogObj>;
10-
private subLoggers: BaseLogger<LogObj>[] = [];
9+
public settings: ISettings<LogObj>;
10+
// not needed yet
11+
//private subLoggers: BaseLogger<LogObj>[] = [];
1112

1213
constructor(settings?: ISettingsParam<LogObj>, private logObj?: LogObj, private stackDepthLevel: number = 4) {
1314
const isBrowser = ![typeof window, typeof document].includes("undefined");
@@ -24,7 +25,8 @@ export class BaseLogger<LogObj> {
2425
minLevel: settings?.minLevel ?? 0,
2526
argumentsArrayName: settings?.argumentsArrayName,
2627
prettyLogTemplate:
27-
settings?.prettyLogTemplate ?? "{{yyyy}}.{{mm}}.{{dd}} {{hh}}:{{MM}}:{{ss}}:{{ms}}\t{{logLevelName}}\t[{{filePathWithLine}}]\t{{name}}",
28+
settings?.prettyLogTemplate ??
29+
"{{yyyy}}.{{mm}}.{{dd}} {{hh}}:{{MM}}:{{ss}}:{{ms}}\t{{logLevelName}}\t[{{filePathWithLine}}{{nameWithDelimiterPrefix}}]\t",
2830
prettyErrorTemplate: settings?.prettyErrorTemplate ?? "\n{{errorName}} {{errorMessage}}\nerror stack:\n{{errorStack}}",
2931
prettyErrorStackTemplate: settings?.prettyErrorTemplate ?? " • {{fileName}}\t{{method}}\n\t{{filePathWithLine}}",
3032
prettyErrorParentNamesSeparator: settings?.prettyErrorParentNamesSeparator ?? ":",
@@ -44,6 +46,8 @@ export class BaseLogger<LogObj> {
4446
dateIsoStr: "white",
4547
filePathWithLine: "white",
4648
name: ["white", "bold"],
49+
nameWithDelimiterPrefix: ["white", "bold"],
50+
nameWithDelimiterSuffix: ["white", "bold"],
4751
errorName: ["bold", "bgRedBright", "whiteBright"],
4852
fileName: ["yellow"],
4953
},
@@ -172,7 +176,7 @@ export class BaseLogger<LogObj> {
172176
logObj?: LogObj,
173177
stackDepthLevel?: number
174178
) => this)(subLoggerSettings, this.logObj, this.stackDepthLevel);
175-
this.subLoggers.push(subLogger);
179+
//this.subLoggers.push(subLogger);
176180
return subLogger;
177181
}
178182

@@ -290,8 +294,11 @@ export class BaseLogger<LogObj> {
290294
// name
291295
let parentNamesString = this.settings.parentNames?.join(this.settings.prettyErrorParentNamesSeparator);
292296
parentNamesString = parentNamesString != null && logObjMeta?.name != null ? parentNamesString + this.settings.prettyErrorParentNamesSeparator : undefined;
293-
const nameStr = logObjMeta?.name != null || parentNamesString != null ? (parentNamesString ?? "") + logObjMeta?.name ?? "" : "";
294-
placeholderValues["name"] = nameStr.length > 0 ? nameStr + this.settings.prettyErrorLoggerNameDelimiter : "";
297+
placeholderValues["name"] = logObjMeta?.name != null || parentNamesString != null ? (parentNamesString ?? "") + logObjMeta?.name ?? "" : "";
298+
placeholderValues["nameWithDelimiterPrefix"] =
299+
placeholderValues["name"].length > 0 ? this.settings.prettyErrorLoggerNameDelimiter + placeholderValues["name"] : "";
300+
placeholderValues["nameWithDelimiterSuffix"] =
301+
placeholderValues["name"].length > 0 ? placeholderValues["name"] + this.settings.prettyErrorLoggerNameDelimiter : "";
295302

296303
return formatTemplate(this.settings, template, placeholderValues);
297304
}

src/interfaces.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,15 @@ export interface ISettingsParam<LogObj> {
3030
ms?: TStyle;
3131
dateIsoStr?: TStyle;
3232
logLevelName?: TStyle;
33+
fileName?: TStyle;
3334
filePath?: TStyle;
3435
fileLine?: TStyle;
36+
filePathWithLine?: TStyle;
37+
name?: TStyle;
38+
nameWithDelimiterPrefix?: TStyle;
39+
nameWithDelimiterSuffix?: TStyle;
40+
errorName?: TStyle;
41+
errorMessage?: TStyle;
3542
};
3643
prettyInspectOptions?: InspectOptions;
3744
metaProperty?: string;
@@ -82,6 +89,8 @@ export interface ISettings<LogObj> extends ISettingsParam<LogObj> {
8289
fileLine?: TStyle;
8390
filePathWithLine?: TStyle;
8491
name?: TStyle;
92+
nameWithDelimiterPrefix?: TStyle;
93+
nameWithDelimiterSuffix?: TStyle;
8594
errorName?: TStyle;
8695
errorMessage?: TStyle;
8796
};

0 commit comments

Comments
 (0)