lumen-log 0.0.0-stage → 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 +33 -2
- package/dist/Logger/colors.d.ts +15 -0
- package/dist/Logger/config.d.ts +2 -0
- package/dist/Logger/fileManager.d.ts +4 -0
- package/dist/Logger/loggerTools.d.ts +2 -0
- package/dist/Logger/lumen.d.ts +15 -0
- package/dist/Logger/telegram.d.ts +2 -0
- package/dist/Logger/themes.d.ts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +414 -0
- package/dist/types/loggerTypes.d.ts +35 -0
- package/package.json +40 -4
package/README.md
CHANGED
|
@@ -1,3 +1,34 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Lumen-log v.0.1.0
|
|
2
|
+
Lumen это библиотка для более широких настроек логирования из "коробки". С подсветкой, типами логов, записью в файлы, дублировани в Telegram Bot.
|
|
3
|
+
|
|
4
|
+
### Функционал
|
|
5
|
+
- Логирование разных типов: log, debug, ok, error, warn.
|
|
6
|
+
- Опциональная запись в файлы и сортировка логов. Lumen сам создаст папку для логов, а внутри для каждого типа лога. Внутри можно увидеть файлы с логами, название файла - дата. (Можно настроить для всех или только для конкретных логов)
|
|
7
|
+
- Опциональное дублирование в Telegram bot. Указав в env api бота и передав массив чатов(ids), логи будут дублироваться в бот. (Можно настроить для всех, или только для конкретных логов)
|
|
8
|
+
- Передача данных в лог. Переданные данные будут отображены в логе, с подсветкой типов, значений, и вложенностью.
|
|
9
|
+
- Настройка логера через конфиг lumen.config.json, и через параметр options если нужно добавить уникальные настройки определенному логеру.
|
|
10
|
+
|
|
11
|
+
### Пример использования
|
|
12
|
+
```ts
|
|
13
|
+
import { Lumen } from 'lumen-log'
|
|
14
|
+
|
|
15
|
+
const main = () => {
|
|
16
|
+
Lumen.log('Hello, Lumen log!')
|
|
17
|
+
Lumen.debug('Hello, Lumen debug!')
|
|
18
|
+
Lumen.error('Hello, Lumen error!')
|
|
19
|
+
Lumen.ok('Hello, Lumen ok!')
|
|
20
|
+
Lumen.warn('Hello, Lumen warning!', {
|
|
21
|
+
options: {
|
|
22
|
+
logInFile: true,
|
|
23
|
+
telegram: {
|
|
24
|
+
logInTelegram: true
|
|
25
|
+
},
|
|
26
|
+
marker: '[!]',
|
|
27
|
+
padding: true
|
|
28
|
+
}
|
|
29
|
+
})
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
main()
|
|
33
|
+
```
|
|
2
34
|
|
|
3
|
-
This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export declare const Colors: {
|
|
2
|
+
readonly reset: "\u001B[0m";
|
|
3
|
+
readonly black: "\u001B[30m";
|
|
4
|
+
readonly red: "\u001B[31m";
|
|
5
|
+
readonly green: "\u001B[32m";
|
|
6
|
+
readonly yellow: "\u001B[33m";
|
|
7
|
+
readonly blue: "\u001B[34m";
|
|
8
|
+
readonly magenta: "\u001B[35m";
|
|
9
|
+
readonly cyan: "\u001B[36m";
|
|
10
|
+
readonly white: "\u001B[37m";
|
|
11
|
+
readonly gray: "\u001B[90m";
|
|
12
|
+
readonly bold: "\u001B[1m";
|
|
13
|
+
readonly underline: "\u001B[4m";
|
|
14
|
+
};
|
|
15
|
+
export declare const colorize: (text: string | number, color: keyof typeof Colors) => string;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { Message, TypeLog } from '../types/loggerTypes.js';
|
|
2
|
+
export declare const getLogsFilesDirectory: () => string;
|
|
3
|
+
export declare const getFilePath: (typeLog: TypeLog, directory: string) => string;
|
|
4
|
+
export declare const printLogInFile: (pathFile: string, message: Message) => void;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { ILoggerProps, Message } from "../types/loggerTypes.js";
|
|
2
|
+
export declare class Lumen {
|
|
3
|
+
private static config;
|
|
4
|
+
private static getInfo;
|
|
5
|
+
private static buildMeta;
|
|
6
|
+
private static buildLog;
|
|
7
|
+
private static buildLogWithoutColorize;
|
|
8
|
+
private static logInFile;
|
|
9
|
+
private static logInTelegram;
|
|
10
|
+
static log(message: Message, props?: ILoggerProps): void;
|
|
11
|
+
static debug(message: Message, props?: ILoggerProps): void;
|
|
12
|
+
static ok(message: Message, props?: ILoggerProps): void;
|
|
13
|
+
static error(message: Message, props?: ILoggerProps): void;
|
|
14
|
+
static warn(message: Message, props?: ILoggerProps): void;
|
|
15
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,414 @@
|
|
|
1
|
+
// src/Logger/colors.ts
|
|
2
|
+
var Colors = {
|
|
3
|
+
reset: "\x1B[0m",
|
|
4
|
+
black: "\x1B[30m",
|
|
5
|
+
red: "\x1B[31m",
|
|
6
|
+
green: "\x1B[32m",
|
|
7
|
+
yellow: "\x1B[33m",
|
|
8
|
+
blue: "\x1B[34m",
|
|
9
|
+
magenta: "\x1B[35m",
|
|
10
|
+
cyan: "\x1B[36m",
|
|
11
|
+
white: "\x1B[37m",
|
|
12
|
+
gray: "\x1B[90m",
|
|
13
|
+
bold: "\x1B[1m",
|
|
14
|
+
underline: "\x1B[4m"
|
|
15
|
+
};
|
|
16
|
+
var colorize = (text, color) => {
|
|
17
|
+
return `${Colors[color]}${text}${Colors.reset}`;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
// src/Logger/loggerTools.ts
|
|
21
|
+
var getDateAndTimeField = (showTime, showDate) => {
|
|
22
|
+
const dateAndTime = /* @__PURE__ */ new Date();
|
|
23
|
+
const dateInfo = {
|
|
24
|
+
day: dateAndTime.getDate(),
|
|
25
|
+
month: dateAndTime.getMonth(),
|
|
26
|
+
year: dateAndTime.getFullYear(),
|
|
27
|
+
h: dateAndTime.getHours(),
|
|
28
|
+
m: dateAndTime.getMinutes(),
|
|
29
|
+
s: dateAndTime.getSeconds()
|
|
30
|
+
};
|
|
31
|
+
const date = showDate ? `${dateInfo.day}.${dateInfo.month}.${dateInfo.year}` : "";
|
|
32
|
+
const time = showTime ? `${dateInfo.h}:${dateInfo.m}:${dateInfo.s}` : "";
|
|
33
|
+
const separator = date && time ? " " : "";
|
|
34
|
+
const field = `[${date}${separator}${time}]`;
|
|
35
|
+
return field;
|
|
36
|
+
};
|
|
37
|
+
var getCallerInfoField = (showNameCaller = true, showFileCaller = true, level = 5) => {
|
|
38
|
+
const stack = new Error().stack?.split("\n");
|
|
39
|
+
const caller = stack?.[level]?.trim();
|
|
40
|
+
const match = caller?.match(/at (.*?) \((.*?):\d+:\d+\)/);
|
|
41
|
+
const functionCallerName = showNameCaller ? (match?.[1] ?? "unknown_function") + "()" : "";
|
|
42
|
+
const fileCallerName = showFileCaller ? match?.[2]?.split(/[\\/]/).pop() ?? "unknown_file" : "";
|
|
43
|
+
const separator = functionCallerName && fileCallerName ? " -> " : "";
|
|
44
|
+
const field = `[${fileCallerName}${separator}${functionCallerName}]`;
|
|
45
|
+
return field;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
// src/Logger/themes.ts
|
|
49
|
+
var getColorForElement = (element, typeLog) => {
|
|
50
|
+
const theme = {
|
|
51
|
+
marker: {
|
|
52
|
+
log: "gray",
|
|
53
|
+
debug: "magenta",
|
|
54
|
+
ok: "green",
|
|
55
|
+
error: "red",
|
|
56
|
+
warn: "yellow"
|
|
57
|
+
},
|
|
58
|
+
date: {
|
|
59
|
+
log: "gray",
|
|
60
|
+
debug: "gray",
|
|
61
|
+
ok: "gray",
|
|
62
|
+
error: "gray",
|
|
63
|
+
warn: "gray"
|
|
64
|
+
},
|
|
65
|
+
caller: {
|
|
66
|
+
log: "gray",
|
|
67
|
+
debug: "gray",
|
|
68
|
+
ok: "gray",
|
|
69
|
+
error: "gray",
|
|
70
|
+
warn: "gray"
|
|
71
|
+
},
|
|
72
|
+
text: {
|
|
73
|
+
log: "white",
|
|
74
|
+
debug: "magenta",
|
|
75
|
+
ok: "green",
|
|
76
|
+
error: "red",
|
|
77
|
+
warn: "yellow"
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
return theme[element][typeLog];
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
// src/Logger/config.ts
|
|
84
|
+
import { existsSync, readFileSync } from "fs";
|
|
85
|
+
import { dirname, join } from "path";
|
|
86
|
+
var defaultConfig = {
|
|
87
|
+
logInFile: false,
|
|
88
|
+
logInFileForDebug: false,
|
|
89
|
+
telegram: {
|
|
90
|
+
logInTelegram: false,
|
|
91
|
+
api: void 0,
|
|
92
|
+
chatsIds: void 0
|
|
93
|
+
},
|
|
94
|
+
nameCaller: true,
|
|
95
|
+
nameFileCaller: true,
|
|
96
|
+
timeCall: true,
|
|
97
|
+
dateCall: true,
|
|
98
|
+
padding: false,
|
|
99
|
+
marker: "\u23FA"
|
|
100
|
+
};
|
|
101
|
+
var isObject = (value) => {
|
|
102
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
103
|
+
};
|
|
104
|
+
var findConfig = () => {
|
|
105
|
+
let currentDir = process.cwd();
|
|
106
|
+
while (true) {
|
|
107
|
+
const configPath = join(currentDir, "lumen.config.json");
|
|
108
|
+
if (existsSync(configPath)) {
|
|
109
|
+
return configPath;
|
|
110
|
+
}
|
|
111
|
+
const parentDir = dirname(currentDir);
|
|
112
|
+
if (parentDir === currentDir) {
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
currentDir = parentDir;
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
var getConfig = () => {
|
|
119
|
+
const configPath = findConfig();
|
|
120
|
+
if (!configPath) {
|
|
121
|
+
return { ...defaultConfig };
|
|
122
|
+
}
|
|
123
|
+
try {
|
|
124
|
+
const fileContent = readFileSync(configPath, "utf-8");
|
|
125
|
+
const parsedConfig = JSON.parse(fileContent);
|
|
126
|
+
if (!isObject(parsedConfig)) {
|
|
127
|
+
throw new Error("Configuration must be a JSON object");
|
|
128
|
+
}
|
|
129
|
+
const telegramConfig = isObject(parsedConfig.telegram) ? parsedConfig.telegram : {};
|
|
130
|
+
return {
|
|
131
|
+
...defaultConfig,
|
|
132
|
+
...parsedConfig,
|
|
133
|
+
telegram: {
|
|
134
|
+
...defaultConfig.telegram,
|
|
135
|
+
...telegramConfig,
|
|
136
|
+
api: process.env.TELEGRAM_BOT_TOKEN ?? telegramConfig.api ?? defaultConfig?.telegram?.api
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
} catch (error) {
|
|
140
|
+
console.error("[Lumen Log] Failed to read config:", error);
|
|
141
|
+
return { ...defaultConfig };
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
// src/Logger/fileManager.ts
|
|
146
|
+
import fs from "fs";
|
|
147
|
+
import path from "path";
|
|
148
|
+
var getLogsFilesDirectory = () => {
|
|
149
|
+
console.log("start search logs directory");
|
|
150
|
+
const dir = process.cwd() + "/logs";
|
|
151
|
+
return dir;
|
|
152
|
+
};
|
|
153
|
+
var getFilePath = (typeLog, directory) => {
|
|
154
|
+
const date = (/* @__PURE__ */ new Date()).toLocaleDateString();
|
|
155
|
+
const dir = path.join(directory, typeLog);
|
|
156
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
157
|
+
const filePath = `${dir}/${date}.log`;
|
|
158
|
+
try {
|
|
159
|
+
fs.writeFileSync(filePath, "", { flag: "wx" });
|
|
160
|
+
} catch (error) {
|
|
161
|
+
if (error.code !== "EEXIST") {
|
|
162
|
+
throw error;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return filePath;
|
|
166
|
+
};
|
|
167
|
+
var printLogInFile = (pathFile, message) => {
|
|
168
|
+
fs.appendFileSync(pathFile, message.toString() + "\n", "utf-8");
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
// src/Logger/telegram.ts
|
|
172
|
+
var sendTelegramMessage = async (message, api, chatIds) => {
|
|
173
|
+
if (!api) {
|
|
174
|
+
throw new Error("[Lumen Log] Telegram bot token is missing");
|
|
175
|
+
}
|
|
176
|
+
if (!chatIds) {
|
|
177
|
+
throw new Error("[Lumen Log] Telegram: No chats ids");
|
|
178
|
+
}
|
|
179
|
+
const url = `https://api.telegram.org/bot${api}/sendMessage`;
|
|
180
|
+
await Promise.all(
|
|
181
|
+
chatIds.map((userId) => sendMessage(url, userId, message))
|
|
182
|
+
);
|
|
183
|
+
};
|
|
184
|
+
var sendMessage = async (url, chatId, message) => {
|
|
185
|
+
const response = await fetch(url, {
|
|
186
|
+
method: "POST",
|
|
187
|
+
headers: {
|
|
188
|
+
"Content-Type": "application/json"
|
|
189
|
+
},
|
|
190
|
+
body: JSON.stringify({
|
|
191
|
+
chat_id: chatId,
|
|
192
|
+
text: String(message)
|
|
193
|
+
})
|
|
194
|
+
});
|
|
195
|
+
const result = await response.json();
|
|
196
|
+
if (!response.ok || !result.ok) {
|
|
197
|
+
throw new Error(result.description ?? "Telegram API error");
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
// src/Logger/lumen.ts
|
|
202
|
+
var Lumen = class {
|
|
203
|
+
// Private
|
|
204
|
+
static config = getConfig();
|
|
205
|
+
static getInfo(props, level = 5) {
|
|
206
|
+
const dateCall = props?.options?.dateCall ?? this.config.dateCall;
|
|
207
|
+
const timeCall = props?.options?.timeCall ?? this.config.timeCall;
|
|
208
|
+
const showDateAndTimeField = dateCall || timeCall;
|
|
209
|
+
const nameCaller = props?.options?.nameCaller ?? this.config.nameCaller;
|
|
210
|
+
const fileCaller = props?.options?.nameFileCaller ?? this.config.nameFileCaller;
|
|
211
|
+
const showCallerField = nameCaller || fileCaller;
|
|
212
|
+
const dateAndTime = showDateAndTimeField ? getDateAndTimeField(timeCall, dateCall) : "";
|
|
213
|
+
const callerInfo = showCallerField ? getCallerInfoField(nameCaller, fileCaller, level) : "";
|
|
214
|
+
return {
|
|
215
|
+
showDateAndTimeField,
|
|
216
|
+
showCallerField,
|
|
217
|
+
dateAndTime,
|
|
218
|
+
callerInfo
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
static buildMeta(meta, options = {}) {
|
|
222
|
+
const { colored = true } = options;
|
|
223
|
+
const lines = ["\n~ Data:", "{"];
|
|
224
|
+
const colorTypes = {
|
|
225
|
+
string: "green",
|
|
226
|
+
number: "yellow",
|
|
227
|
+
boolean: "red",
|
|
228
|
+
undefined: "gray"
|
|
229
|
+
};
|
|
230
|
+
const indent = (depth) => " ".repeat(depth);
|
|
231
|
+
const getType = (value) => {
|
|
232
|
+
if (value === null) return "null";
|
|
233
|
+
if (Array.isArray(value)) return "array";
|
|
234
|
+
return typeof value;
|
|
235
|
+
};
|
|
236
|
+
const paint = (value, color) => colored ? colorize(value, color) : value;
|
|
237
|
+
const formatKey = (key) => paint(key, "blue") === key || !colored ? colored ? colorize(colorize(key, "blue"), "bold") : key : colorize(colorize(key, "blue"), "bold");
|
|
238
|
+
const formatType = (type) => paint(`(${type})`, "magenta");
|
|
239
|
+
const formatValue = (value) => {
|
|
240
|
+
if (typeof value === "string") return JSON.stringify(value);
|
|
241
|
+
if (typeof value === "undefined") return "undefined";
|
|
242
|
+
if (typeof value === "bigint") return `${value}n`;
|
|
243
|
+
if (typeof value === "symbol") return value.toString();
|
|
244
|
+
if (typeof value === "function") {
|
|
245
|
+
return `[Function: ${value.name || "anonymous"}]`;
|
|
246
|
+
}
|
|
247
|
+
return String(value);
|
|
248
|
+
};
|
|
249
|
+
const visited = /* @__PURE__ */ new WeakSet();
|
|
250
|
+
const render = (value, key, depth) => {
|
|
251
|
+
const prefix = indent(depth);
|
|
252
|
+
const formattedKey = colored ? colorize(colorize(key, "blue"), "bold") : key;
|
|
253
|
+
if (value !== null && typeof value === "object") {
|
|
254
|
+
if (visited.has(value)) {
|
|
255
|
+
lines.push(`${prefix}${formattedKey}: [Circular]`);
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
visited.add(value);
|
|
259
|
+
try {
|
|
260
|
+
if (Array.isArray(value)) {
|
|
261
|
+
lines.push(
|
|
262
|
+
`${prefix}${formattedKey}: [ ${formatType(`array, length: ${value.length}`)}`
|
|
263
|
+
);
|
|
264
|
+
value.forEach((item, index) => {
|
|
265
|
+
render(item, `[${index}]`, depth + 1);
|
|
266
|
+
});
|
|
267
|
+
lines.push(`${prefix}]`);
|
|
268
|
+
} else {
|
|
269
|
+
lines.push(
|
|
270
|
+
`${prefix}${formattedKey}: { ${formatType("object")}`
|
|
271
|
+
);
|
|
272
|
+
for (const [childKey, childValue] of Object.entries(value)) {
|
|
273
|
+
render(childValue, childKey, depth + 1);
|
|
274
|
+
}
|
|
275
|
+
lines.push(`${prefix}}`);
|
|
276
|
+
}
|
|
277
|
+
} finally {
|
|
278
|
+
visited.delete(value);
|
|
279
|
+
}
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
const type = getType(value);
|
|
283
|
+
const color = colorTypes[type] ?? "white";
|
|
284
|
+
const formattedValue = paint(formatValue(value), color);
|
|
285
|
+
lines.push(
|
|
286
|
+
`${prefix}${formattedKey}: ${formattedValue} ${formatType(type)}`
|
|
287
|
+
);
|
|
288
|
+
};
|
|
289
|
+
if (meta !== null && typeof meta === "object" && !Array.isArray(meta)) {
|
|
290
|
+
for (const [key, value] of Object.entries(meta)) {
|
|
291
|
+
render(value, key, 1);
|
|
292
|
+
}
|
|
293
|
+
} else {
|
|
294
|
+
render(meta, "value", 1);
|
|
295
|
+
}
|
|
296
|
+
lines.push("}");
|
|
297
|
+
return lines.join("\n");
|
|
298
|
+
}
|
|
299
|
+
static buildLog(dataLog) {
|
|
300
|
+
const colorMarker = getColorForElement("marker", dataLog.logType);
|
|
301
|
+
const colorDate = getColorForElement("date", dataLog.logType);
|
|
302
|
+
const colorCaller = getColorForElement("caller", dataLog.logType);
|
|
303
|
+
const colorText = getColorForElement("text", dataLog.logType);
|
|
304
|
+
const { dateAndTime, callerInfo } = this.getInfo(dataLog.props);
|
|
305
|
+
const separator = dateAndTime || callerInfo ? " " : "";
|
|
306
|
+
const padding = dataLog.props?.options?.padding ?? this.config.padding ? "\n" : "";
|
|
307
|
+
const markerSymbol = dataLog.props?.options?.marker ?? this.config.marker ?? "\u23FA";
|
|
308
|
+
const marker = `${colorize(markerSymbol, colorMarker)} `;
|
|
309
|
+
const loggerType = colorize(`[${dataLog.logType.toUpperCase()}]`, colorMarker);
|
|
310
|
+
const fields = `${colorize(dateAndTime, colorDate)}${separator}${colorize(callerInfo, colorCaller)}`;
|
|
311
|
+
const textForMessage = colorize(dataLog.message, colorText);
|
|
312
|
+
const metaData = dataLog.props?.data;
|
|
313
|
+
const metaField = metaData ? this.buildMeta(metaData) : "";
|
|
314
|
+
const text = `${padding}${marker} ${loggerType} ${fields}${separator ? "\n" : ""}${textForMessage}${metaField}${padding}`;
|
|
315
|
+
return text;
|
|
316
|
+
}
|
|
317
|
+
static buildLogWithoutColorize(dataLog) {
|
|
318
|
+
const { dateAndTime, callerInfo } = this.getInfo(dataLog.props, 6);
|
|
319
|
+
const separator = dateAndTime || callerInfo ? " " : "";
|
|
320
|
+
const padding = dataLog.props?.options?.padding ?? this.config.padding ? "\n" : "";
|
|
321
|
+
const marker = dataLog.props?.options?.marker ?? "\u23FA";
|
|
322
|
+
const loggerType = `[${dataLog.logType.toUpperCase()}]`;
|
|
323
|
+
const fields = `${dateAndTime}${separator}${callerInfo}`;
|
|
324
|
+
const metaData = dataLog.props?.data;
|
|
325
|
+
const metaField = metaData ? this.buildMeta(metaData, { colored: false }) : "";
|
|
326
|
+
const text = `${padding}${marker} ${loggerType} ${fields}${separator ? "\n" : ""}${dataLog.message}${metaField}${padding}`;
|
|
327
|
+
return text;
|
|
328
|
+
}
|
|
329
|
+
static logInFile(dataLog) {
|
|
330
|
+
const canWrite = dataLog.props?.options?.logInFile ?? this.config.logInFile;
|
|
331
|
+
const isDebug = dataLog.logType === "debug";
|
|
332
|
+
const isWriteDebug = dataLog.props?.options?.logInFileForDebug;
|
|
333
|
+
if (!canWrite) {
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
if (isDebug && !isWriteDebug) {
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
339
|
+
const directory = getLogsFilesDirectory();
|
|
340
|
+
const filePath = getFilePath(dataLog.logType, directory);
|
|
341
|
+
const textForLog = this.buildLogWithoutColorize(dataLog);
|
|
342
|
+
printLogInFile(filePath, textForLog);
|
|
343
|
+
}
|
|
344
|
+
static logInTelegram(dataLog) {
|
|
345
|
+
const canSend = dataLog.props?.options?.telegram?.logInTelegram ?? this.config.telegram?.logInTelegram;
|
|
346
|
+
const api = dataLog.props?.options?.telegram?.api ?? this.config.telegram?.api;
|
|
347
|
+
const chatsIds = dataLog.props?.options?.telegram?.chatsIds ?? this.config.telegram?.chatsIds;
|
|
348
|
+
const canSendToTelegram = canSend && api && chatsIds;
|
|
349
|
+
if (!canSendToTelegram) {
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
const textMessage = this.buildLogWithoutColorize(dataLog);
|
|
353
|
+
sendTelegramMessage(textMessage, api, chatsIds);
|
|
354
|
+
}
|
|
355
|
+
// Public functions
|
|
356
|
+
static log(message, props) {
|
|
357
|
+
const dataLog = {
|
|
358
|
+
message,
|
|
359
|
+
logType: "log",
|
|
360
|
+
props
|
|
361
|
+
};
|
|
362
|
+
const textLog = this.buildLog(dataLog);
|
|
363
|
+
console.log(textLog);
|
|
364
|
+
this.logInFile(dataLog);
|
|
365
|
+
this.logInTelegram(dataLog);
|
|
366
|
+
}
|
|
367
|
+
static debug(message, props) {
|
|
368
|
+
const dataLog = {
|
|
369
|
+
message,
|
|
370
|
+
logType: "debug",
|
|
371
|
+
props
|
|
372
|
+
};
|
|
373
|
+
const textLog = this.buildLog(dataLog);
|
|
374
|
+
console.log(textLog);
|
|
375
|
+
this.logInFile(dataLog);
|
|
376
|
+
this.logInTelegram(dataLog);
|
|
377
|
+
}
|
|
378
|
+
static ok(message, props) {
|
|
379
|
+
const dataLog = {
|
|
380
|
+
message,
|
|
381
|
+
logType: "ok",
|
|
382
|
+
props
|
|
383
|
+
};
|
|
384
|
+
const textLog = this.buildLog(dataLog);
|
|
385
|
+
console.log(textLog);
|
|
386
|
+
this.logInFile(dataLog);
|
|
387
|
+
this.logInTelegram(dataLog);
|
|
388
|
+
}
|
|
389
|
+
static error(message, props) {
|
|
390
|
+
const dataLog = {
|
|
391
|
+
message,
|
|
392
|
+
logType: "error",
|
|
393
|
+
props
|
|
394
|
+
};
|
|
395
|
+
const textLog = this.buildLog(dataLog);
|
|
396
|
+
console.log(textLog);
|
|
397
|
+
this.logInFile(dataLog);
|
|
398
|
+
this.logInTelegram(dataLog);
|
|
399
|
+
}
|
|
400
|
+
static warn(message, props) {
|
|
401
|
+
const dataLog = {
|
|
402
|
+
message,
|
|
403
|
+
logType: "warn",
|
|
404
|
+
props
|
|
405
|
+
};
|
|
406
|
+
const textLog = this.buildLog(dataLog);
|
|
407
|
+
console.log(textLog);
|
|
408
|
+
this.logInFile(dataLog);
|
|
409
|
+
this.logInTelegram(dataLog);
|
|
410
|
+
}
|
|
411
|
+
};
|
|
412
|
+
export {
|
|
413
|
+
Lumen
|
|
414
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { Colors } from "../Logger/colors.ts";
|
|
2
|
+
interface ITelegram {
|
|
3
|
+
logInTelegram?: boolean;
|
|
4
|
+
api?: string | undefined;
|
|
5
|
+
chatsIds?: Array<number> | undefined;
|
|
6
|
+
}
|
|
7
|
+
export interface ILumenOptions {
|
|
8
|
+
logInFile?: boolean;
|
|
9
|
+
logInFileForDebug?: boolean;
|
|
10
|
+
telegram?: ITelegram;
|
|
11
|
+
nameCaller?: boolean;
|
|
12
|
+
nameFileCaller?: boolean;
|
|
13
|
+
timeCall?: boolean;
|
|
14
|
+
dateCall?: boolean;
|
|
15
|
+
padding?: boolean;
|
|
16
|
+
marker?: string;
|
|
17
|
+
}
|
|
18
|
+
export interface ILoggerProps {
|
|
19
|
+
options?: ILumenOptions;
|
|
20
|
+
data?: object;
|
|
21
|
+
}
|
|
22
|
+
export interface ILog {
|
|
23
|
+
message: Message;
|
|
24
|
+
logType: TypeLog;
|
|
25
|
+
props?: ILoggerProps | undefined;
|
|
26
|
+
}
|
|
27
|
+
export interface IMetaOptions {
|
|
28
|
+
colored?: boolean;
|
|
29
|
+
}
|
|
30
|
+
export type Color = keyof typeof Colors;
|
|
31
|
+
export type ITheme = Record<ElementLog, Record<TypeLog, Color>>;
|
|
32
|
+
export type Message = string | number;
|
|
33
|
+
export type TypeLog = 'log' | 'debug' | 'ok' | 'error' | 'warn';
|
|
34
|
+
export type ElementLog = 'marker' | 'date' | 'caller' | 'text';
|
|
35
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,42 @@
|
|
|
1
|
+
|
|
1
2
|
{
|
|
2
3
|
"name": "lumen-log",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"description": "A customizable logger for Node.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js"
|
|
13
|
+
},
|
|
14
|
+
"./package.json": "./package.json"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"README.md"
|
|
19
|
+
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "tsup src/index.ts --format esm --clean --out-dir dist && tsc",
|
|
22
|
+
"prepublishOnly": "npm run build"
|
|
23
|
+
},
|
|
24
|
+
"keywords": [
|
|
25
|
+
"logger",
|
|
26
|
+
"logging",
|
|
27
|
+
"nodejs",
|
|
28
|
+
"typescript",
|
|
29
|
+
"console",
|
|
30
|
+
"lumen-log"
|
|
31
|
+
],
|
|
32
|
+
"author": "ButkovKirill",
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"engines": {
|
|
35
|
+
"node": ">=18"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@types/node": "^26.6.6",
|
|
39
|
+
"tsup": "^8.5.1",
|
|
40
|
+
"typescript": "^6.0.3"
|
|
41
|
+
}
|
|
42
|
+
}
|