Skip to content

Commit 6ab687e

Browse files
authored
Merge pull request #257 from fullstack-build/development
Development
2 parents 0e751a5 + c24f7e1 commit 6ab687e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+4912
-489
lines changed

README.md

+18-1
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,9 @@ export class CustomLogger<LogObj> extends BaseLogger<LogObj> {
250250
/**
251251
* Logs a _CUSTOM_ message.
252252
* @param args - Multiple log attributes that should be logged.
253+
* @return LogObject with meta property, when log level is >= minLevel
253254
*/
254-
public custom(...args: unknown[]): LogObj & ILogObjMeta {
255+
public custom(...args: unknown[]): LogObj & ILogObjMeta | undefined {
255256
return super.log(8, "CUSTOM", ...args);
256257
}
257258

@@ -453,6 +454,22 @@ Following settings are available for styling:
453454
- `prettyErrorLoggerNameDelimiter`: if a logger name is set this delimiter will be added afterwards
454455
- `prettyInspectOptions`: <a href="https://nodejs.org/api/util.html#utilinspectobject-options" target="_blank">Available options</a>
455456

457+
### Customizing template tokens
458+
It's possible to add user defined tokes, by overwriting the `addPlaceholders` in the `settings.overwrite`. this callback allows to add or overwrite tokens in the `placeholderValues`.
459+
for example, to add the token: `{{custom}}`;
460+
```javascript
461+
const logger = new Logger({
462+
type: "pretty",
463+
prettyLogTemplate: "{{custom}} ",
464+
overwrite: {
465+
addPlaceholders: (logObjMeta: IMeta, placeholderValues: Record<string, string>) => {
466+
placeholderValues["custom"] = "test";
467+
},
468+
},
469+
});
470+
```
471+
this would yield in the token `{{custom}}` being replaced with `"test"`
472+
456473
- **Styling:**
457474
- `stylePrettyLogs`: defines whether logs should be styled and colorized
458475
- `prettyLogStyles`: provides colors and styles for different placeholders and can also be dependent on the value (e.g. log level)

build.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { build } from "esbuild";
22

33
build({
4-
entryPoints: ["src/index.ts"],
4+
entryPoints: ["src/index.browser.ts"],
55
outfile: "dist/browser/index.js",
66
platform: "browser",
77
bundle: true,

docs/README.md

+18-1
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,9 @@ export class CustomLogger<LogObj> extends BaseLogger<LogObj> {
250250
/**
251251
* Logs a _CUSTOM_ message.
252252
* @param args - Multiple log attributes that should be logged.
253+
* @return LogObject with meta property, when log level is >= minLevel
253254
*/
254-
public custom(...args: unknown[]): LogObj & ILogObjMeta {
255+
public custom(...args: unknown[]): LogObj & ILogObjMeta | undefined {
255256
return super.log(8, "CUSTOM", ...args);
256257
}
257258

@@ -453,6 +454,22 @@ Following settings are available for styling:
453454
- `prettyErrorLoggerNameDelimiter`: if a logger name is set this delimiter will be added afterwards
454455
- `prettyInspectOptions`: <a href="https://nodejs.org/api/util.html#utilinspectobject-options" target="_blank">Available options</a>
455456

457+
### Customizing template tokens
458+
It's possible to add user defined tokes, by overwriting the `addPlaceholders` in the `settings.overwrite`. this callback allows to add or overwrite tokens in the `placeholderValues`.
459+
for example, to add the token: `{{custom}}`;
460+
```javascript
461+
const logger = new Logger({
462+
type: "pretty",
463+
prettyLogTemplate: "{{custom}} ",
464+
overwrite: {
465+
addPlaceholders: (logObjMeta: IMeta, placeholderValues: Record<string, string>) => {
466+
placeholderValues["custom"] = "test";
467+
},
468+
},
469+
});
470+
```
471+
this would yield in the token `{{custom}}` being replaced with `"test"`
472+
456473
- **Styling:**
457474
- `stylePrettyLogs`: defines whether logs should be styled and colorized
458475
- `prettyLogStyles`: provides colors and styles for different placeholders and can also be dependent on the value (e.g. log level)

docs/index.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
<html lang="en">
33
<head>
44
<meta charset="UTF-8">
5-
<title>tslog - 📝 Extensible TypeScript Logger for Node.js and Browser: Dependency free, Fully customizable, Pretty errors, stack traces, and JSON output to attachable transports.</title>
5+
<title>tslog - Extensible TypeScript Logger for Node.js and Browser.</title>
66
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
7-
<meta name="description" content="📝 Extensible TypeScript Logger for Node.js and Browser: Dependency free, Fully customizable, Pretty errors, stack traces, and JSON output to attachable transports.">
7+
<meta name="description" content="Extensible TypeScript Logger for Node.js and Browser.">
88
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
99
<link rel="stylesheet" href="//unpkg.com/docsify/lib/themes/vue.css">
1010
</head>

examples/nodejs/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Logger, ILogObj } from "../../src";
1+
import { Logger, ILogObj } from "../../src/index.js";
22

33
class MyClass {
44
private readonly _logger: Logger<ILogObj> = new Logger({

examples/nodejs/index2.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Logger, BaseLogger, IMeta } from "../../src/index.js";
1+
import { Logger, BaseLogger } from "../../src/index.js";
22

33
const defaultLogObject: {
44
name: string;

examples/nodejs/mongodb/index.ts

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { MongoClient } from "mongodb";
2+
import { Logger, ILogObj } from "tslog";
3+
4+
interface ITestData {
5+
_id: string;
6+
testList: string[];
7+
}
8+
9+
const log: Logger<ILogObj> = new Logger();
10+
11+
const dbOperate = async (col: any, id: string, testId: string) => {
12+
const firstResult = await col.findOneAndUpdate(
13+
{ _id: id, testList: { $not: { $eq: testId } } },
14+
{
15+
$push: {
16+
testList: {
17+
$each: [testId],
18+
$slice: 10,
19+
},
20+
},
21+
},
22+
{
23+
upsert: true,
24+
projection: { testList: 1 },
25+
returnDocument: "after",
26+
}
27+
);
28+
};
29+
30+
const main = async (): Promise<void> => {
31+
const mongoClient = new MongoClient("mongodb://127.0.0.1:27017", {
32+
family: 4,
33+
noDelay: true,
34+
connectTimeoutMS: 5000,
35+
});
36+
37+
await mongoClient.connect();
38+
39+
const db = mongoClient.db("test");
40+
const col = db.collection<ITestData>("test_col");
41+
const id = "10001";
42+
const testId = "1001";
43+
44+
// delete key may already exist
45+
await col.deleteOne({ _id: id });
46+
47+
// should ok
48+
const firstResult = await dbOperate(col, id, testId);
49+
log.info("first result", firstResult);
50+
51+
try {
52+
const secondResult = await dbOperate(col, id, testId); // trigger duplicate key error
53+
log.info("second result", secondResult);
54+
} catch (error) {
55+
console.log("error", error);
56+
log.error("second result", error); //traceback here
57+
}
58+
};
59+
60+
main();

0 commit comments

Comments
 (0)