Skip to content

Commit

Permalink
Merge pull request #257 from fullstack-build/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
terehov committed Aug 8, 2023
2 parents 0e751a5 + c24f7e1 commit 6ab687e
Show file tree
Hide file tree
Showing 55 changed files with 4,912 additions and 489 deletions.
19 changes: 18 additions & 1 deletion README.md
Expand Up @@ -250,8 +250,9 @@ export class CustomLogger<LogObj> extends BaseLogger<LogObj> {
/**
* Logs a _CUSTOM_ message.
* @param args - Multiple log attributes that should be logged.
* @return LogObject with meta property, when log level is >= minLevel
*/
public custom(...args: unknown[]): LogObj & ILogObjMeta {
public custom(...args: unknown[]): LogObj & ILogObjMeta | undefined {
return super.log(8, "CUSTOM", ...args);
}

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

### Customizing template tokens
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`.
for example, to add the token: `{{custom}}`;
```javascript
const logger = new Logger({
type: "pretty",
prettyLogTemplate: "{{custom}} ",
overwrite: {
addPlaceholders: (logObjMeta: IMeta, placeholderValues: Record<string, string>) => {
placeholderValues["custom"] = "test";
},
},
});
```
this would yield in the token `{{custom}}` being replaced with `"test"`

- **Styling:**
- `stylePrettyLogs`: defines whether logs should be styled and colorized
- `prettyLogStyles`: provides colors and styles for different placeholders and can also be dependent on the value (e.g. log level)
Expand Down
2 changes: 1 addition & 1 deletion build.js
@@ -1,7 +1,7 @@
import { build } from "esbuild";

build({
entryPoints: ["src/index.ts"],
entryPoints: ["src/index.browser.ts"],
outfile: "dist/browser/index.js",
platform: "browser",
bundle: true,
Expand Down
19 changes: 18 additions & 1 deletion docs/README.md
Expand Up @@ -250,8 +250,9 @@ export class CustomLogger<LogObj> extends BaseLogger<LogObj> {
/**
* Logs a _CUSTOM_ message.
* @param args - Multiple log attributes that should be logged.
* @return LogObject with meta property, when log level is >= minLevel
*/
public custom(...args: unknown[]): LogObj & ILogObjMeta {
public custom(...args: unknown[]): LogObj & ILogObjMeta | undefined {
return super.log(8, "CUSTOM", ...args);
}

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

### Customizing template tokens
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`.
for example, to add the token: `{{custom}}`;
```javascript
const logger = new Logger({
type: "pretty",
prettyLogTemplate: "{{custom}} ",
overwrite: {
addPlaceholders: (logObjMeta: IMeta, placeholderValues: Record<string, string>) => {
placeholderValues["custom"] = "test";
},
},
});
```
this would yield in the token `{{custom}}` being replaced with `"test"`

- **Styling:**
- `stylePrettyLogs`: defines whether logs should be styled and colorized
- `prettyLogStyles`: provides colors and styles for different placeholders and can also be dependent on the value (e.g. log level)
Expand Down
4 changes: 2 additions & 2 deletions docs/index.html
Expand Up @@ -2,9 +2,9 @@
<html lang="en">
<head>
<meta charset="UTF-8">
<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>
<title>tslog - Extensible TypeScript Logger for Node.js and Browser.</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<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.">
<meta name="description" content="Extensible TypeScript Logger for Node.js and Browser.">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<link rel="stylesheet" href="//unpkg.com/docsify/lib/themes/vue.css">
</head>
Expand Down
2 changes: 1 addition & 1 deletion examples/nodejs/index.ts
@@ -1,4 +1,4 @@
import { Logger, ILogObj } from "../../src";
import { Logger, ILogObj } from "../../src/index.js";

class MyClass {
private readonly _logger: Logger<ILogObj> = new Logger({
Expand Down
2 changes: 1 addition & 1 deletion examples/nodejs/index2.ts
@@ -1,4 +1,4 @@
import { Logger, BaseLogger, IMeta } from "../../src/index.js";
import { Logger, BaseLogger } from "../../src/index.js";

const defaultLogObject: {
name: string;
Expand Down
60 changes: 60 additions & 0 deletions examples/nodejs/mongodb/index.ts
@@ -0,0 +1,60 @@
import { MongoClient } from "mongodb";
import { Logger, ILogObj } from "tslog";

interface ITestData {
_id: string;
testList: string[];
}

const log: Logger<ILogObj> = new Logger();

const dbOperate = async (col: any, id: string, testId: string) => {
const firstResult = await col.findOneAndUpdate(
{ _id: id, testList: { $not: { $eq: testId } } },
{
$push: {
testList: {
$each: [testId],
$slice: 10,
},
},
},
{
upsert: true,
projection: { testList: 1 },
returnDocument: "after",
}
);
};

const main = async (): Promise<void> => {
const mongoClient = new MongoClient("mongodb://127.0.0.1:27017", {
family: 4,
noDelay: true,
connectTimeoutMS: 5000,
});

await mongoClient.connect();

const db = mongoClient.db("test");
const col = db.collection<ITestData>("test_col");
const id = "10001";
const testId = "1001";

// delete key may already exist
await col.deleteOne({ _id: id });

// should ok
const firstResult = await dbOperate(col, id, testId);
log.info("first result", firstResult);

try {
const secondResult = await dbOperate(col, id, testId); // trigger duplicate key error
log.info("second result", secondResult);
} catch (error) {
console.log("error", error);
log.error("second result", error); //traceback here
}
};

main();

0 comments on commit 6ab687e

Please sign in to comment.