Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
6b01ec5e19
|
|||
|
539ebf01d9
|
|||
|
30970d257d
|
+3
-3
@@ -1,6 +1,6 @@
|
||||
bun.lockb
|
||||
package-lock.json
|
||||
node_modules/
|
||||
.idea/
|
||||
._*
|
||||
.idea/
|
||||
.DS_Store
|
||||
node_modules/
|
||||
dist/
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
.woodpecker/
|
||||
test/
|
||||
.gitignore
|
||||
README.md
|
||||
.prettierignore
|
||||
.prettierrc
|
||||
eslint.config.ts
|
||||
@@ -0,0 +1,4 @@
|
||||
.woodpecker
|
||||
node_modules
|
||||
test
|
||||
dist
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"arrowParens": "avoid",
|
||||
"tabWidth": 2,
|
||||
"useTabs": false,
|
||||
"bracketSpacing": false,
|
||||
"printWidth": 120,
|
||||
"semi": false,
|
||||
"singleQuote": true,
|
||||
"jsxSingleQuote": true,
|
||||
"trailingComma": "none"
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
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
|
||||
@@ -0,0 +1,8 @@
|
||||
# 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/*"]`
|
||||
@@ -0,0 +1,20 @@
|
||||
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'])
|
||||
])
|
||||
@@ -1,5 +0,0 @@
|
||||
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}
|
||||
+26
-12
@@ -1,21 +1,35 @@
|
||||
{
|
||||
"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",
|
||||
"files": ["index.ts"],
|
||||
|
||||
"dependencies": {
|
||||
"chalk": "^5.3.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/chalk": "^2.2.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"typescript": "^5.0.0"
|
||||
"main": "./src/index.ts",
|
||||
"exports": {
|
||||
".": "./src/index.ts"
|
||||
}
|
||||
}
|
||||
|
||||
+13
-8
@@ -1,14 +1,19 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
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}
|
||||
+59
-57
@@ -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 let options: Options = {
|
||||
format: "[$time] $level $namespace :",
|
||||
export const writers = new Map<string, Writer>()
|
||||
export const options: Options = {
|
||||
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: unknown[]): void {
|
||||
log(data, Level.DEBUG, this._namespace)
|
||||
}
|
||||
|
||||
public debug(...data: any[]) : void {
|
||||
log(data, Level.DEBUG, this._namespace)
|
||||
}
|
||||
public trace(...data: unknown[]): void {
|
||||
log(data, Level.TRACE, this._namespace)
|
||||
}
|
||||
|
||||
public trace(...data: any[]) : void {
|
||||
log(data, Level.TRACE, this._namespace)
|
||||
}
|
||||
public info(...data: unknown[]): void {
|
||||
log(data, Level.INFO, this._namespace)
|
||||
}
|
||||
|
||||
public info(...data: any[]) : void {
|
||||
log(data, Level.INFO, this._namespace)
|
||||
}
|
||||
public warn(...data: unknown[]): 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: unknown[]): void {
|
||||
log(data, Level.ERROR, this._namespace)
|
||||
}
|
||||
}
|
||||
|
||||
function log(message: any[], level: Level, namespace: string) : void {
|
||||
if (writers.size === 0) return
|
||||
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)
|
||||
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
@@ -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]: unknown
|
||||
}
|
||||
|
||||
export interface Writer {
|
||||
log(level: Level, ...data: any[]) : void;
|
||||
get options() : WriterOptions;
|
||||
readonly _options: WriterOptions;
|
||||
log(level: Level, ...data: unknown[]): void
|
||||
get options(): WriterOptions
|
||||
readonly _options: WriterOptions
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
import {test} from 'bun:test'
|
||||
|
||||
test('Basic test', () => 'Hello, world!')
|
||||
+8
-27
@@ -1,33 +1,14 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
// Enable latest features
|
||||
"lib": ["ESNext","dom"],
|
||||
"target": "ESNext",
|
||||
"target": "ES2020",
|
||||
"module": "ESNext",
|
||||
"moduleDetection": "force",
|
||||
"allowJs": true,
|
||||
"checkJs": true,
|
||||
|
||||
// Bundler mode
|
||||
"moduleResolution": "bundler",
|
||||
"allowImportingTsExtensions": true,
|
||||
"verbatimModuleSyntax": true,
|
||||
"noEmit": true,
|
||||
|
||||
// Best practices
|
||||
"moduleResolution": "node",
|
||||
"declaration": true,
|
||||
"outDir": "dist",
|
||||
"rootDir": "./src",
|
||||
"strict": true,
|
||||
"skipLibCheck": true,
|
||||
"noFallthroughCasesInSwitch": true,
|
||||
|
||||
// Some stricter flags (disabled by default)
|
||||
"noUnusedLocals": false,
|
||||
"noUnusedParameters": false,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"noPropertyAccessFromIndexSignature": false
|
||||
"esModuleInterop": true,
|
||||
"skipLibCheck": true
|
||||
},
|
||||
|
||||
"include": [
|
||||
"index.ts",
|
||||
"src/**/*.ts",
|
||||
]
|
||||
"include": ["src"]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user