13 Commits

Author SHA1 Message Date
pascal 73e9f804c4 Fix export of Writer 2024-05-15 21:10:40 +02:00
pascal 69d93bcf61 Replace Writer abstract class with interface 2024-05-15 21:04:28 +02:00
pascal 208e7834d3 remove useless failproofing 2024-05-15 21:04:08 +02:00
pascal 802bd8e511 Update Writer interface 2024-05-15 20:46:06 +02:00
pascal cb9b082285 Unify package.json 2024-05-15 13:15:07 +02:00
pascal 42c80676e7 Failsafe options 2024-05-15 12:54:36 +02:00
pascal 2af2f19efb Fix Writer's constructor's privacy 2024-05-15 12:46:55 +02:00
pascal c948aa3cc2 Fix various issues (import style, type on Writer) 2024-05-14 23:09:11 +02:00
pascal 17ac2bdcf4 Remove writers from lib
Closes #4
2024-05-14 22:49:59 +02:00
pascal 4dbe36932c Unify lib options 2024-05-14 12:51:44 +02:00
pascal 8ca1f9e2f2 implement lib 2024-05-14 12:46:32 +02:00
pascal 903a315788 Hide src from package 2024-05-14 12:46:32 +02:00
pascal 3b452ae9d7 Add structure 2024-05-14 12:46:31 +02:00
15 changed files with 144 additions and 215 deletions
+4 -4
View File
@@ -1,6 +1,6 @@
bun.lockb
package-lock.json
._*
.idea/
.DS_Store
node_modules/
dist/
.idea/
._*
.DS_Store
-7
View File
@@ -1,7 +0,0 @@
.woodpecker/
test/
.gitignore
README.md
.prettierignore
.prettierrc
eslint.config.ts
-4
View File
@@ -1,4 +0,0 @@
.woodpecker
node_modules
test
dist
-11
View File
@@ -1,11 +0,0 @@
{
"arrowParens": "avoid",
"tabWidth": 2,
"useTabs": false,
"bracketSpacing": false,
"printWidth": 120,
"semi": false,
"singleQuote": true,
"jsxSingleQuote": true,
"trailingComma": "none"
}
-19
View File
@@ -1,19 +0,0 @@
when:
- path:
include: ['src/**/*.ts']
steps:
install:
image: node
when:
- event: [pull_request, push, manual]
commands:
- npm install
test:
image: oven/bun:alpine
when:
- event: [pull_request, push, manual]
depends_on: install
commands:
- bun test
-8
View File
@@ -1,8 +0,0 @@
# Library template
Basic template to create a TypeScript library that works properly when included
## How to split in multiple file
Technically, there is nothing to do, but if you want to allow sub-imports, you have to :
1. In `package.json`, under `exports`, add `"./submodule": "./src/submodule.ts"`
2. In `tsconfig.json`, under `paths`, add `"lib-name/*": ["./src/*"]`
-20
View File
@@ -1,20 +0,0 @@
import js from '@eslint/js'
import globals from 'globals'
import tseslint from 'typescript-eslint'
import {defineConfig, globalIgnores} from 'eslint/config'
export default defineConfig([
tseslint.configs.recommended,
{
files: ['**/*.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
plugins: {js},
extends: ['js/recommended'],
languageOptions: {globals: {...globals.browser, ...globals.node}},
rules: {
'no-unused-vars': 'warn',
'@typescript-eslint/no-unused-vars': ['warn']
}
},
globalIgnores(['dist'])
])
+5
View File
@@ -0,0 +1,5 @@
import {Level, LevelFilter, 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}
+12 -26
View File
@@ -1,35 +1,21 @@
{
"scripts": {
"check": "clear ; npm run typecheck && npm run lint && npx prettier -c **/*.{js,jsx,ts,tsx} && clear && echo 'OK'",
"fmt": "prettier --write **/*.{js,jsx,ts,tsx}",
"lint": "eslint **/*.{js,jsx,ts,tsx}",
"test": "bun test",
"typecheck": "tsc --noEmit"
},
"dependencies": {
"chalk": "^5.6.2"
},
"devDependencies": {
"@eslint/js": "^9.35.0",
"@types/bun": "^1.2.21",
"bun": "^1.2.21",
"eslint": "^9.35.0",
"globals": "^16.4.0",
"jiti": "^2.5.1",
"prettier": "^3.6.2",
"typescript": "^5.9.2",
"typescript-eslint": "^8.43.0"
},
"name": "log",
"description": "log library with logger and writers",
"version": "1.0.0",
"author": "Pascal Perrenoud <pascal@pband.ch>",
"module": "index.ts",
"type": "module",
"main": "./src/index.ts",
"exports": {
".": "./src/index.ts"
"files": ["index.ts"],
"dependencies": {
"chalk": "^5.3.0"
},
"devDependencies": {
"@types/chalk": "^2.2.0"
},
"peerDependencies": {
"typescript": "^5.0.0"
}
}
+8 -13
View File
@@ -1,19 +1,14 @@
import {Chalk} from 'chalk'
import {Level} from './types'
const chalk = new Chalk({level: 2}) // 256 colors
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
}
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
}
}
-5
View File
@@ -1,5 +0,0 @@
import {Level, type WriterOptions, type Writer} from './types'
import {Logger, writers, options} from './logger'
export default (namespace: string): Logger => new Logger(namespace)
export {Logger, Level, type WriterOptions, type Writer, writers, options}
+67 -69
View File
@@ -1,79 +1,77 @@
import {Level, type Options, type Writer} from './types'
import {get_color} from './color'
export const writers = new Map<string, Writer>()
export const options: Options = {
format: '[$time] $level $namespace :',
pad_level: true,
verbose: true
export const writers = new Map<string, Writer>
export let options: Options = {
format: "[$time] $level $namespace :",
}
export class Logger {
constructor(private readonly _namespace: string) {}
private readonly _namespace: string
public get namespace(): string {
return this._namespace
}
public extend(sub_namespace: string): Logger {
return new Logger(`${this.namespace}:${sub_namespace}`)
}
public debug(...data: unknown[]): void {
log(data, Level.DEBUG, this._namespace)
}
public trace(...data: unknown[]): void {
log(data, Level.TRACE, this._namespace)
}
public info(...data: unknown[]): void {
log(data, Level.INFO, this._namespace)
}
public warn(...data: unknown[]): void {
log(data, Level.WARNING, this._namespace)
}
public error(...data: unknown[]): void {
log(data, Level.ERROR, this._namespace)
}
}
function log(message: unknown[], 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)
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
constructor(namespace: string) {
this._namespace = namespace
}
public get namespace() : string {
return this._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 trace(...data: any[]) : void {
log(data, Level.TRACE, 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 error(...data: any[]) : void {
log(data, Level.ERROR, this._namespace)
}
writer.log(level, head_bw, ...message)
}
}
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 log(message: any[], level: Level, namespace: string) : void {
if (writers.size === 0) 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))
for (const writer of writers.values()) {
writer.log(level, writer.options?.with_color ? head_color : 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 "
}
}
+21 -18
View File
@@ -1,29 +1,32 @@
/**
* Global options for loggers
*
* Format : can contain $time, $level and $namespace
*/
export type Options = {
format: string
pad_level: boolean
verbose: boolean
format: string,
}
export enum Level {
TRACE = 0,
DEBUG = 1,
INFO = 2,
WARNING = 3,
ERROR = 4
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,
}
export type WriterOptions = {
minLevel: Level
[key: string | number | symbol]: unknown
minLevel: Level,
with_color: boolean,
[key: string | number | symbol]: any,
}
export interface Writer {
log(level: Level, ...data: unknown[]): void
get options(): WriterOptions
readonly _options: WriterOptions
log(level: Level, ...data: any[]) : void;
get options() : WriterOptions;
readonly _options: WriterOptions;
}
-3
View File
@@ -1,3 +0,0 @@
import {test} from 'bun:test'
test('Basic test', () => 'Hello, world!')
+27 -8
View File
@@ -1,14 +1,33 @@
{
"compilerOptions": {
"target": "ES2020",
// Enable latest features
"lib": ["ESNext","dom"],
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "node",
"declaration": true,
"outDir": "dist",
"rootDir": "./src",
"moduleDetection": "force",
"allowJs": true,
"checkJs": true,
// Bundler mode
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"noEmit": true,
// Best practices
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true
"skipLibCheck": true,
"noFallthroughCasesInSwitch": true,
// Some stricter flags (disabled by default)
"noUnusedLocals": false,
"noUnusedParameters": false,
"forceConsistentCasingInFileNames": true,
"noPropertyAccessFromIndexSignature": false
},
"include": ["src"]
"include": [
"index.ts",
"src/**/*.ts",
]
}