forked from openiap/opencore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogger.ts
More file actions
88 lines (86 loc) · 3.02 KB
/
Copy pathLogger.ts
File metadata and controls
88 lines (86 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import * as winston from "winston";
import { Config } from "./Config";
const path = require('path');
import { format } from 'winston';
import { i_otel } from "./commoninterfaces";
const MAX_RETRIES_DEFAULT = 5
export async function promiseRetry<T>(
fn: () => Promise<T>,
retries = MAX_RETRIES_DEFAULT,
retryIntervalMillis: number,
previousError?: Error
): Promise<T> {
return !retries
? Promise.reject(previousError)
: fn().catch(async (error) => {
await new Promise((resolve) => setTimeout(resolve, retryIntervalMillis))
return promiseRetry(fn, retries - 1, retryIntervalMillis, error)
})
}
// Object.defineProperty(Promise, 'retry', {
// configurable: true,
// writable: true,
// value: function retry(retries, executor) {
// // console.warn(`${retries} retries left!`)
// if (typeof retries !== 'number') {
// throw new TypeError('retries is not a number')
// }
// return new Promise(executor).catch(error => {
// retries > 0 ? (Promise as any).retry(retries - 1, executor) : Promise.reject(error);
// }
// )
// }
// })
export class Logger {
public static otel: i_otel;
static configure(): winston.Logger {
const filename = path.join(Config.logpath, "nodered" + Config.nodered_id + ".log");
const options: any = {
file: {
level: "debug",
filename: filename,
handleExceptions: false,
json: false,
maxsize: 5242880, // 5MB
maxFiles: 5,
colorize: false,
},
console: {
level: "debug",
handleExceptions: false,
json: false,
colorize: true
},
};
const myFormat = winston.format.printf(info => {
if (info instanceof Error || info.stack) {
return `${info.timestamp} [${info.level}] ${info.message} \n ${info.stack}`;
}
return `${info.timestamp} [${info.level}] ${info.message}`;
});
options.console.format = format.combine(
winston.format.errors({ stack: true }),
winston.format.timestamp({ format: 'HH:mm:ss' }),
winston.format.colorize(),
winston.format.json(),
myFormat
);
const logger: winston.Logger = winston.createLogger({
level: "debug",
//format: winston.format.json(),
format: winston.format.combine(
winston.format.errors({ stack: true }),
winston.format.timestamp({ format: 'HH:mm:ss' }),
winston.format.json(),
myFormat
),
transports: [
new winston.transports.File(options.file),
new winston.transports.Console(options.console)
]
});
Logger.instanse = logger;
return logger;
}
static instanse: winston.Logger = null;
}