Add log function

This commit is contained in:
2026-01-05 01:23:06 +00:00
parent 6d9631d181
commit fe99f4fd7e
6 changed files with 64 additions and 3 deletions

18
package-lock.json generated
View File

@@ -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"
}
}
}

View File

@@ -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
View File

View File

@@ -0,0 +1 @@
import { log } from './logger'

39
src/logger.ts Normal file
View 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);
}
}

View File

@@ -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"],