@wlix/logger 1.0.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 +26 -0
- package/index.d.ts +33 -0
- package/index.js +76 -0
- package/package.json +21 -0
package/README.md
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# @wlix/logger
|
|
2
|
+
|
|
3
|
+
A tiny file-based logger for Node.js projects. It writes logs to daily files and can also print them to stdout.
|
|
4
|
+
|
|
5
|
+
## How it works
|
|
6
|
+
|
|
7
|
+
- When you create a `Logger`, it ensures the logs directory exists.
|
|
8
|
+
- Each day gets its own log file named `DD-MM-YYYY.log`.
|
|
9
|
+
- Every log line includes UTC time, log level, and your message.
|
|
10
|
+
- Logs are then written to the file automatically.
|
|
11
|
+
|
|
12
|
+
## Usage
|
|
13
|
+
|
|
14
|
+
- Instantiate a new Logger instance.
|
|
15
|
+
- Use `Logger#info()`, `Logger#warn()`, or `Logger#error()` with a message.
|
|
16
|
+
- Files are written to the `./logs` dir by default.
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { Logger } from "./Logger";
|
|
20
|
+
|
|
21
|
+
const logger = new Logger({ logToStdout: true });
|
|
22
|
+
|
|
23
|
+
logger.info("Connected to server");
|
|
24
|
+
logger.warn("Memory is low");
|
|
25
|
+
logger.error("Something went wrong");
|
|
26
|
+
```
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export type Timezone = "UTC" | `UTC${"" | "+" | "-"}${number}`;
|
|
2
|
+
|
|
3
|
+
export interface LoggerConfig {
|
|
4
|
+
/**
|
|
5
|
+
* Path of the directory with log files.
|
|
6
|
+
* @default "./logs"
|
|
7
|
+
*/
|
|
8
|
+
logsPath?: string;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Whether to log to stdout.
|
|
12
|
+
* @default true
|
|
13
|
+
*/
|
|
14
|
+
logToStdout?: boolean;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* UTC, or UTC offsets only.
|
|
18
|
+
* @example "UTC"
|
|
19
|
+
* @example "UTC-6"
|
|
20
|
+
* @example "UTC+7"
|
|
21
|
+
* @default "UTC"
|
|
22
|
+
*/
|
|
23
|
+
timezone?: Timezone;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Creates a logger function.
|
|
28
|
+
* @param config Optional logger configuration.
|
|
29
|
+
* @returns A function to log messages: log(type, message)
|
|
30
|
+
*/
|
|
31
|
+
export declare function Logger(
|
|
32
|
+
config?: LoggerConfig
|
|
33
|
+
): (type: "info" | "warn" | "error", message: string) => void;
|
package/index.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
const { existsSync, mkdirSync, appendFileSync } = require("fs");
|
|
2
|
+
const { join } = require("path");
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @typedef {"UTC" | `UTC${"" | "+" | "-"}${number}`} Timezone
|
|
6
|
+
* @typedef {Object} LoggerConfig
|
|
7
|
+
* @property {string} [logsPath] Path of the directory with log files. Default: "./logs"
|
|
8
|
+
* @property {boolean} [logToStdout] Whether to log to stdout. Default: true
|
|
9
|
+
* @property {Timezone} [timezone] UTC, or UTC offsets only. Default: "UTC"
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Creates a logger function.
|
|
14
|
+
* @param {LoggerConfig} config Optional logger configuration.
|
|
15
|
+
* @returns {(type: "info" | "warn" | "error", message: string) => void} Function to log messages
|
|
16
|
+
*/
|
|
17
|
+
function Logger(config = {}) {
|
|
18
|
+
const logsPath = config.logsPath ?? "./logs";
|
|
19
|
+
const logToStdout = config.logToStdout ?? true;
|
|
20
|
+
const timezone = config.timezone ?? "UTC";
|
|
21
|
+
|
|
22
|
+
if (!existsSync(logsPath)) {
|
|
23
|
+
mkdirSync(logsPath, { recursive: true });
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Returns a Date object adjusted for the given timezone.
|
|
28
|
+
* @param {Date} date
|
|
29
|
+
* @param {Timezone} tz
|
|
30
|
+
* @returns {Date}
|
|
31
|
+
*/
|
|
32
|
+
function getOffsetDate(date, tz) {
|
|
33
|
+
if (tz === "UTC") return date;
|
|
34
|
+
|
|
35
|
+
const match = tz.match(/^UTC([+-]\d{1,2})$/);
|
|
36
|
+
if (!match) return date;
|
|
37
|
+
|
|
38
|
+
const offsetHours = parseInt(match[1], 10);
|
|
39
|
+
const utc = date.getTime() + date.getTimezoneOffset() * 60000;
|
|
40
|
+
return new Date(utc + offsetHours * 3600000);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Logs a message to a file and optionally to stdout.
|
|
45
|
+
* @param {"info" | "warn" | "error"} type
|
|
46
|
+
* @param {string} message
|
|
47
|
+
*/
|
|
48
|
+
function log(type, message) {
|
|
49
|
+
const now = getOffsetDate(new Date(), timezone);
|
|
50
|
+
|
|
51
|
+
const fileName = [
|
|
52
|
+
String(now.getDate()).padStart(2, "0"),
|
|
53
|
+
String(now.getMonth() + 1).padStart(2, "0"),
|
|
54
|
+
now.getFullYear(),
|
|
55
|
+
].join("-");
|
|
56
|
+
|
|
57
|
+
const time = [
|
|
58
|
+
String(now.getHours()).padStart(2, "0"),
|
|
59
|
+
String(now.getMinutes()).padStart(2, "0"),
|
|
60
|
+
String(now.getSeconds()).padStart(2, "0"),
|
|
61
|
+
].join(":");
|
|
62
|
+
|
|
63
|
+
const line = `[${time} ${timezone}] [${type.toUpperCase()}] ${message}\n`;
|
|
64
|
+
const filePath = join(logsPath, `${fileName}.log`);
|
|
65
|
+
|
|
66
|
+
appendFileSync(filePath, line);
|
|
67
|
+
|
|
68
|
+
if (logToStdout) {
|
|
69
|
+
console.log(line.trim());
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return log;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
module.exports = { Logger };
|
package/package.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@wlix/logger",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A simple logger.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"types": "index.d.ts",
|
|
7
|
+
"private": false,
|
|
8
|
+
"files": [
|
|
9
|
+
"index.js",
|
|
10
|
+
"index.d.ts",
|
|
11
|
+
"README.md"
|
|
12
|
+
],
|
|
13
|
+
"publishConfig": {
|
|
14
|
+
"access": "public"
|
|
15
|
+
},
|
|
16
|
+
"author": "wlix",
|
|
17
|
+
"license": "ISC",
|
|
18
|
+
"scripts": {
|
|
19
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
20
|
+
}
|
|
21
|
+
}
|