From 55cc0c9d778f0201c046702613d2c952f5392332 Mon Sep 17 00:00:00 2001 From: Pascal Perrenoud Date: Tue, 14 May 2024 23:07:50 +0200 Subject: [PATCH] init Counter writer --- .gitignore | 6 ++++++ index.ts | 33 +++++++++++++++++++++++++++++++++ package.json | 18 ++++++++++++++++++ tsconfig.json | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 89 insertions(+) create mode 100644 .gitignore create mode 100644 index.ts create mode 100644 package.json create mode 100644 tsconfig.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fa5646b --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +bun.lockb +package-lock.json +node_modules/ +.idea/ +._* +.DS_Store diff --git a/index.ts b/index.ts new file mode 100644 index 0000000..6cd5896 --- /dev/null +++ b/index.ts @@ -0,0 +1,33 @@ +import logger, {Writer, Level} from 'log' + +export * as log from 'log' +export default logger + +export class Counter extends Writer { + private readonly calls = { + debug: 0, + trace: 0, + info: 0, + warn: 0, + error: 0, + } + + public constructor() { + super({minLevel: Level.DEBUG, with_color: false}); + } + + protected write(level: Level, ..._data: any[]): void { + switch (level) { + case Level.DEBUG: this.calls.debug++; break + case Level.TRACE: this.calls.trace++; break + case Level.INFO: this.calls.info++; break + case Level.WARNING: this.calls.warn++; break + case Level.ERROR: this.calls.error++; break + default: throw new Error(`Unknown level: ${level}`) + } + } + + public get count() { + return this.calls + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..b1e0019 --- /dev/null +++ b/package.json @@ -0,0 +1,18 @@ +{ + "name": "logger-counter", + "description": "Dummy logger that only counts logs sent, used for testing purposes.", + "version": "1.0.0", + + "author": "Pascal Perrenoud ", + + "module": "index.ts", + "type": "module", + "files": ["index.ts"], + + "dependencies": { + "log": "git+git@git.pband.ch:typescript/log" + }, + "peerDependencies": { + "typescript": "^5.0.0" + } +} diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..3d49bf0 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,32 @@ +{ + "compilerOptions": { + // Enable latest features + "lib": ["ESNext","dom"], + "target": "ESNext", + "module": "ESNext", + "moduleDetection": "force", + "allowJs": true, + "checkJs": true, + + // Bundler mode + "moduleResolution": "bundler", + "allowImportingTsExtensions": true, + "verbatimModuleSyntax": true, + "noEmit": true, + + // Best practices + "strict": true, + "skipLibCheck": true, + "noFallthroughCasesInSwitch": true, + + // Some stricter flags (disabled by default) + "noUnusedLocals": false, + "noUnusedParameters": false, + "forceConsistentCasingInFileNames": true, + "noPropertyAccessFromIndexSignature": false + }, + + "include": [ + "index.ts", + ] +}