@swipe72/logger 0.1.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 +59 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +40 -0
- package/dist/index.js.map +1 -0
- package/package.json +39 -0
package/README.md
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# `@swipe72/logger`
|
|
2
|
+
|
|
3
|
+
A small logger built on top of @swipe72/event-bus.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { createEventBus } from "@swipe72/event-bus";
|
|
9
|
+
import { createLogger } from "@swipe72/logger";
|
|
10
|
+
|
|
11
|
+
const events = createEventBus<{
|
|
12
|
+
"log.created": {
|
|
13
|
+
level: "info" | "warn" | "error";
|
|
14
|
+
scope: string;
|
|
15
|
+
message: string;
|
|
16
|
+
timestamp: number;
|
|
17
|
+
};
|
|
18
|
+
}>();
|
|
19
|
+
|
|
20
|
+
events.on("log.created", (log) => {
|
|
21
|
+
console.log(`[${log.level}] [${log.scope}] ${log.message}`);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
const logger = createLogger({ events, scope: "app" });
|
|
25
|
+
await logger.info("Application started");
|
|
26
|
+
await logger.warn("Something looks suspicious");
|
|
27
|
+
await logger.error("Something went wrong");
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Child loggers
|
|
31
|
+
|
|
32
|
+
Child loggers let you create nested scopes while reusing the same event bus.
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
const logger = createLogger({
|
|
36
|
+
events,
|
|
37
|
+
scope: "app",
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const authLogger = logger.child("auth");
|
|
41
|
+
|
|
42
|
+
await authLogger.info("User signed in");
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
This emits a log event with the scope:
|
|
46
|
+
|
|
47
|
+
`"app.auth"`
|
|
48
|
+
|
|
49
|
+
## API
|
|
50
|
+
|
|
51
|
+
`createLogger(options)`
|
|
52
|
+
|
|
53
|
+
`logger.info(message)`
|
|
54
|
+
|
|
55
|
+
`logger.warn(message)`
|
|
56
|
+
|
|
57
|
+
`logger.error(message)`
|
|
58
|
+
|
|
59
|
+
`logger.child(scope)`
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
type LogLevel = "info" | "warn" | "error";
|
|
2
|
+
type LogPayload = {
|
|
3
|
+
level: LogLevel;
|
|
4
|
+
scope: string;
|
|
5
|
+
message: string;
|
|
6
|
+
timestamp: number;
|
|
7
|
+
};
|
|
8
|
+
type LoggerEvents = {
|
|
9
|
+
"log.created": LogPayload;
|
|
10
|
+
};
|
|
11
|
+
type LoggerEventTarget = {
|
|
12
|
+
emit(event: "log.created", payload: LogPayload): Promise<void>;
|
|
13
|
+
};
|
|
14
|
+
type CreateLoggerOptions<Scope extends string> = {
|
|
15
|
+
events: LoggerEventTarget;
|
|
16
|
+
scope: Scope;
|
|
17
|
+
};
|
|
18
|
+
type Logger<Scope extends string = string> = {
|
|
19
|
+
scope: Scope;
|
|
20
|
+
info(message: string): Promise<void>;
|
|
21
|
+
warn(message: string): Promise<void>;
|
|
22
|
+
error(message: string): Promise<void>;
|
|
23
|
+
child<const ChildScope extends string>(scope: ChildScope): Logger<`${Scope}.${ChildScope}`>;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
declare function createLogger<const Scope extends string>({ events, scope, }: CreateLoggerOptions<Scope>): Logger<Scope>;
|
|
27
|
+
|
|
28
|
+
export { type CreateLoggerOptions, type LogLevel, type LogPayload, type Logger, type LoggerEventTarget, type LoggerEvents, createLogger };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// src/create-logger.ts
|
|
2
|
+
function createLogger({
|
|
3
|
+
events,
|
|
4
|
+
scope
|
|
5
|
+
}) {
|
|
6
|
+
async function log(level, message) {
|
|
7
|
+
await events.emit("log.created", {
|
|
8
|
+
level,
|
|
9
|
+
scope,
|
|
10
|
+
message,
|
|
11
|
+
timestamp: Date.now()
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
function info(message) {
|
|
15
|
+
return log("info", message);
|
|
16
|
+
}
|
|
17
|
+
function warn(message) {
|
|
18
|
+
return log("warn", message);
|
|
19
|
+
}
|
|
20
|
+
function error(message) {
|
|
21
|
+
return log("error", message);
|
|
22
|
+
}
|
|
23
|
+
function child(childScope) {
|
|
24
|
+
return createLogger({
|
|
25
|
+
events,
|
|
26
|
+
scope: `${scope}.${childScope}`
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
return {
|
|
30
|
+
scope,
|
|
31
|
+
info,
|
|
32
|
+
warn,
|
|
33
|
+
error,
|
|
34
|
+
child
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
export {
|
|
38
|
+
createLogger
|
|
39
|
+
};
|
|
40
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/create-logger.ts"],"sourcesContent":["import type { Logger, LogLevel, CreateLoggerOptions } from \"./types\";\n\nexport function createLogger<const Scope extends string>({\n events,\n scope,\n}: CreateLoggerOptions<Scope>): Logger<Scope> {\n /**\n * Log an event\n *\n * @param level\n * @param message\n */\n async function log(level: LogLevel, message: string) {\n await events.emit(\"log.created\", {\n level,\n scope,\n message,\n timestamp: Date.now(),\n });\n }\n\n /**\n * Log an info event\n *\n * @param message\n */\n function info(message: string) {\n return log(\"info\", message);\n }\n\n /**\n * Log an warn event\n *\n * @param message\n */\n function warn(message: string) {\n return log(\"warn\", message);\n }\n\n /**\n * Log an error event\n *\n * @param message\n * @returns\n */\n function error(message: string) {\n return log(\"error\", message);\n }\n\n function child<const ChildScope extends string>(\n childScope: ChildScope,\n ): Logger<`${Scope}.${ChildScope}`> {\n return createLogger({\n events,\n scope: `${scope}.${childScope}` as `${Scope}.${ChildScope}`,\n });\n }\n\n return {\n scope,\n info,\n warn,\n error,\n child,\n };\n}\n"],"mappings":";AAEO,SAAS,aAAyC;AAAA,EACvD;AAAA,EACA;AACF,GAA8C;AAO5C,iBAAe,IAAI,OAAiB,SAAiB;AACnD,UAAM,OAAO,KAAK,eAAe;AAAA,MAC/B;AAAA,MACA;AAAA,MACA;AAAA,MACA,WAAW,KAAK,IAAI;AAAA,IACtB,CAAC;AAAA,EACH;AAOA,WAAS,KAAK,SAAiB;AAC7B,WAAO,IAAI,QAAQ,OAAO;AAAA,EAC5B;AAOA,WAAS,KAAK,SAAiB;AAC7B,WAAO,IAAI,QAAQ,OAAO;AAAA,EAC5B;AAQA,WAAS,MAAM,SAAiB;AAC9B,WAAO,IAAI,SAAS,OAAO;AAAA,EAC7B;AAEA,WAAS,MACP,YACkC;AAClC,WAAO,aAAa;AAAA,MAClB;AAAA,MACA,OAAO,GAAG,KAAK,IAAI,UAAU;AAAA,IAC/B,CAAC;AAAA,EACH;AAEA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@swipe72/logger",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"import": "./dist/index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist"
|
|
15
|
+
],
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public"
|
|
18
|
+
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"vitest": "^4.1.7"
|
|
21
|
+
},
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "git+https://github.com/swipe72/npm-packages.git",
|
|
25
|
+
"directory": "packages/logger"
|
|
26
|
+
},
|
|
27
|
+
"homepage": "https://github.com/swipe72/npm-packages/tree/main/packages/logger",
|
|
28
|
+
"bugs": {
|
|
29
|
+
"url": "https://github.com/swipe72/npm-packages/issues"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "tsup",
|
|
33
|
+
"test": "vitest",
|
|
34
|
+
"lint": "eslint .",
|
|
35
|
+
"lint:fix": "eslint . --fix",
|
|
36
|
+
"format": "prettier . --write --ignore-path ../../.prettierignore",
|
|
37
|
+
"format:check": "prettier . --check --ignore-path ../../.prettierignore"
|
|
38
|
+
}
|
|
39
|
+
}
|