15 Commits

Author SHA1 Message Date
pascal 9418bfe6ba Remove problematic lib chalk 2025-09-06 09:57:22 +02:00
pascal 347608ca40 Add tsconfig to lib 2024-07-29 11:54:00 +02:00
Pascal Perrenoud 436cea462d remove peerDependencies 2024-07-27 13:38:49 +02:00
Pascal Perrenoud abc41ac749 fmt 2024-07-27 13:35:17 +02:00
Pascal Perrenoud 27779a07f0 BREAKING CHANGE : Align levels to Rust :) 2024-06-03 16:55:58 +02:00
pascal 7a4284ef6a Rework padding option 2024-05-22 10:14:20 +02:00
pascal 140382073d Add comment 2024-05-22 10:12:49 +02:00
pascal 216225b11a Remove LevelFilter 2024-05-22 10:12:17 +02:00
pascal 0daf6497d5 Allow to control padding of level 2024-05-22 10:11:59 +02:00
pascal fae54e1b71 Add verbose option 2024-05-22 10:01:03 +02:00
pascal ca3528268a Remove types chalk as suggested 2024-05-22 10:00:54 +02:00
pascal 7d0a87a2eb Unify constructor 2024-05-22 08:52:15 +02:00
pascal 87f5865915 Do a check no one did ??? 2024-05-20 01:15:05 +02:00
pascal 5f76dd89a3 Type logger and export Logger 2024-05-19 19:37:16 +02:00
pascal ca87ffd33c Export all files for NPM 2024-05-19 19:37:10 +02:00
5 changed files with 80 additions and 105 deletions
+3 -3
View File
@@ -1,5 +1,5 @@
import {Level, LevelFilter, type WriterOptions, type Writer} from './src/types'
import {Level, type WriterOptions, type Writer} from './src/types'
import {Logger, writers, options} from './src/logger'
export default (namespace: string) => new Logger(namespace)
export {Level, LevelFilter, type WriterOptions, type Writer, writers, options}
export default (namespace: string): Logger => new Logger(namespace)
export {Logger, Level, type WriterOptions, type Writer, writers, options}
+1 -11
View File
@@ -7,15 +7,5 @@
"module": "index.ts",
"type": "module",
"files": ["index.ts"],
"dependencies": {
"chalk": "^5.3.0"
},
"devDependencies": {
"@types/chalk": "^2.2.0"
},
"peerDependencies": {
"typescript": "^5.0.0"
}
"files": ["index.ts", "src", "tsconfig.json"]
}
-14
View File
@@ -1,14 +0,0 @@
import {Chalk} from 'chalk'
import {Level} from './types'
const chalk = new Chalk({ level: 2 }) // 256 colors
export function get_color(level: Level) {
switch (level) {
case Level.DEBUG: return chalk.blueBright
case Level.TRACE: return chalk.green
case Level.INFO: return (str: string) => str
case Level.WARNING: return chalk.hex('#FFA500')
case Level.ERROR: return chalk.red
}
}
+58 -56
View File
@@ -1,77 +1,79 @@
import {Level, type Options, type Writer} from './types'
import {get_color} from './color'
export const writers = new Map<string, Writer>
export const writers = new Map<string, Writer>()
export let options: Options = {
format: "[$time] $level $namespace :",
format: '[$time] $level $namespace :',
pad_level: true,
verbose: true
}
export class Logger {
private readonly _namespace: string
constructor(private readonly _namespace: string) {}
constructor(namespace: string) {
this._namespace = namespace
}
public get namespace(): string {
return this._namespace
}
public get namespace() : string {
return this._namespace
}
public extend(sub_namespace: string): Logger {
return new Logger(`${this.namespace}:${sub_namespace}`)
}
public extend(sub_namespace: string) : Logger {
return new Logger(`${this.namespace}:${sub_namespace}`)
}
public debug(...data: any[]): void {
log(data, Level.DEBUG, this._namespace)
}
public debug(...data: any[]) : void {
log(data, Level.DEBUG, this._namespace)
}
public trace(...data: any[]): void {
log(data, Level.TRACE, this._namespace)
}
public trace(...data: any[]) : void {
log(data, Level.TRACE, this._namespace)
}
public info(...data: any[]): void {
log(data, Level.INFO, this._namespace)
}
public info(...data: any[]) : void {
log(data, Level.INFO, this._namespace)
}
public warn(...data: any[]): void {
log(data, Level.WARNING, this._namespace)
}
public warn(...data: any[]) : void {
log(data, Level.WARNING, this._namespace)
}
public error(...data: any[]) : void {
log(data, Level.ERROR, this._namespace)
}
public error(...data: any[]): void {
log(data, Level.ERROR, this._namespace)
}
}
function log(message: any[], level: Level, namespace: string) : void {
if (writers.size === 0) return
function log(message: any[], level: Level, namespace: string): void {
if (writers.size === 0) {
if (options.verbose) console.log('No writer subscribed, discard message')
return
}
// Format header of log
const head = options.format
.replace("$time", new Date().toISOString())
.replace("$namespace", namespace)
const head_bw = head.replace('$level', level_to_string(level, false))
const head_color = head.replace("$level", level_to_string(level, true))
// Format header of log
const head = options.format.replace('$time', new Date().toISOString()).replace('$namespace', namespace)
for (const writer of writers.values()) {
writer.log(level, writer.options?.with_color ? head_color : head_bw, ...message)
let lvl = get_string(level)
if (!options.pad_level) lvl = lvl.trimEnd()
const head_bw = head.replace('$level', lvl)
for (const [name, writer] of writers.entries()) {
const options = writer.options
if (options?.minLevel > level) {
if (options.verbose) console.log(`Writer's level is lower, discard message for ${name}`)
continue
}
writer.log(level, head_bw, ...message)
}
}
function level_to_string(level: Level, with_color: boolean) : string {
const str = get_string(level)
if (!with_color) return str
const color = get_color(level)
return color(str)
function get_string(level: Level): string {
switch (level) {
case Level.DEBUG:
return 'DEBUG '
case Level.TRACE:
return 'TRACE '
case Level.INFO:
return 'INFO '
case Level.WARNING:
return 'WARNING'
case Level.ERROR:
return 'ERROR '
}
}
function get_string(level: Level) : string {
switch (level) {
case Level.DEBUG: return "DEBUG "
case Level.TRACE: return "TRACE "
case Level.INFO: return "INFO "
case Level.WARNING: return "WARNING"
case Level.ERROR: return "ERROR "
}
}
+18 -21
View File
@@ -1,32 +1,29 @@
/**
* Global options for loggers
*
* Format : can contain $time, $level and $namespace
*/
export type Options = {
format: string,
format: string
pad_level: boolean
verbose: boolean
}
export enum Level {
DEBUG = 0,
TRACE = 1,
INFO = 2,
WARNING = 3,
ERROR = 4,
}
export enum LevelFilter {
DEBUG = 0,
TRACE = 1,
INFO = 2,
WARNING = 3,
ERROR = 4,
OFF = 6,
TRACE = 0,
DEBUG = 1,
INFO = 2,
WARNING = 3,
ERROR = 4
}
export type WriterOptions = {
minLevel: Level,
with_color: boolean,
[key: string | number | symbol]: any,
minLevel: Level
[key: string | number | symbol]: any
}
export interface Writer {
log(level: Level, ...data: any[]) : void;
get options() : WriterOptions;
readonly _options: WriterOptions;
log(level: Level, ...data: any[]): void
get options(): WriterOptions
readonly _options: WriterOptions
}