Add log function
This commit is contained in:
18
package-lock.json
generated
18
package-lock.json
generated
@@ -12,6 +12,7 @@
|
||||
"@owlboard/backend-data-contracts": "^0.0.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^25.0.3",
|
||||
"typescript": "^5.9.3"
|
||||
}
|
||||
},
|
||||
@@ -21,6 +22,16 @@
|
||||
"integrity": "sha512-uUjz/EEdCEnW0is3s1QiFvFfeVk1FcaBVGL+atfwo8lMWqTwQfLwKbG7qZNtvLNp2U8GabDWkf2srM86A20KlA==",
|
||||
"license": "ISC"
|
||||
},
|
||||
"node_modules/@types/node": {
|
||||
"version": "25.0.3",
|
||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-25.0.3.tgz",
|
||||
"integrity": "sha512-W609buLVRVmeW693xKfzHeIV6nJGGz98uCPfeXI1ELMLXVeKYZ9m15fAMSaUPBHYLGFsVRcMmSCksQOrZV9BYA==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"undici-types": "~7.16.0"
|
||||
}
|
||||
},
|
||||
"node_modules/typescript": {
|
||||
"version": "5.9.3",
|
||||
"resolved": "https://registry.npmjs.org/typescript/-/typescript-5.9.3.tgz",
|
||||
@@ -34,6 +45,13 @@
|
||||
"engines": {
|
||||
"node": ">=14.17"
|
||||
}
|
||||
},
|
||||
"node_modules/undici-types": {
|
||||
"version": "7.16.0",
|
||||
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-7.16.0.tgz",
|
||||
"integrity": "sha512-Zz+aZWSj8LE6zoxD+xrjh4VfkIG8Ya6LvYkZqtUQGJPZjYl53ypCaUwWqo7eI0x66KBGeRo+mlBEkMSeSZ38Nw==",
|
||||
"dev": true,
|
||||
"license": "MIT"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
"@owlboard/backend-data-contracts": "^0.0.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^25.0.3",
|
||||
"typescript": "^5.9.3"
|
||||
}
|
||||
}
|
||||
|
||||
0
src/gitea.ts
Normal file
0
src/gitea.ts
Normal file
@@ -0,0 +1 @@
|
||||
import { log } from './logger'
|
||||
|
||||
39
src/logger.ts
Normal file
39
src/logger.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
type LogLevel = 'DEBUG' | 'INFO' | 'WARN' | 'ERROR';
|
||||
const colours: Record<LogLevel, string> = {
|
||||
DEBUG: '\x1b[36m', // Cyan
|
||||
INFO: '\x1b[32m', // Green
|
||||
WARN: '\x1b[33m', // Yellow
|
||||
ERROR: '\x1b[31m', // Red
|
||||
};
|
||||
|
||||
const levelWeight: Record<LogLevel, number> = {
|
||||
DEBUG: 0,
|
||||
INFO: 1,
|
||||
WARN: 2,
|
||||
ERROR: 3,
|
||||
};
|
||||
|
||||
const RESET = '\x1b[0m';
|
||||
|
||||
const CURRENT_LOG_LEVEL = (process.env.LOG_LEVEL as LogLevel) || 'INFO';
|
||||
const MIN_WEIGHT = levelWeight[CURRENT_LOG_LEVEL] ?? 1;
|
||||
|
||||
export function log(level: LogLevel, msg: string, ...args: any[]): void {
|
||||
// Drop log if below LOG_LEVEL
|
||||
if (levelWeight[level] < MIN_WEIGHT) return;
|
||||
|
||||
const timestamp = new Date().toISOString();
|
||||
const colour = colours[level] || RESET;
|
||||
|
||||
const header = `[${timestamp}] [${colour}${level.padEnd(5)}${RESET}]: ${msg}`;
|
||||
|
||||
if (args.length > 0) {
|
||||
if (level === 'ERROR') {
|
||||
console.error(header, ...args);
|
||||
} else {
|
||||
console.log(header, ...args);
|
||||
}
|
||||
} else {
|
||||
level === 'ERROR' ? console.error(header) : console.log(header);
|
||||
}
|
||||
}
|
||||
@@ -7,10 +7,12 @@
|
||||
|
||||
// Environment Settings
|
||||
// See also https://aka.ms/tsconfig/module
|
||||
"module": "commonjs",
|
||||
"target": "es2020",
|
||||
"module": "esnext",
|
||||
"target": "es2022",
|
||||
"esModuleInterop": true,
|
||||
"types": [],
|
||||
"types": ["node"],
|
||||
"moduleResolution": "bundler",
|
||||
"allowSyntheticDefaultImports": true,
|
||||
// For nodejs:
|
||||
// "lib": ["esnext"],
|
||||
// "types": ["node"],
|
||||
|
||||
Reference in New Issue
Block a user