From 135950f31b2b3a3b33d2eff963b137c6e6da54d0 Mon Sep 17 00:00:00 2001 From: Kyle Carbone Date: Mon, 16 Jan 2023 23:30:20 -0500 Subject: [PATCH] first stable release! --- package.json | 10 ++++++---- src/actions.ts | 2 +- src/config.ts | 14 ++++++++++---- src/index.ts | 6 +++--- tests/index.test.ts | 4 ++-- 5 files changed, 22 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index 6ef9f41..b0d8c0f 100644 --- a/package.json +++ b/package.json @@ -1,14 +1,16 @@ { "name": "bark-logger", - "version": "0.0.5", + "version": "1.0.0", "description": "Simple log framework for browser and server", "main": "lib/index.js", "types": "lib/index.d.ts", "scripts": { - "start": "tsc -w", - "test": "tsc && jest build/tests/*", "build": "tsc", - "clean": "rm -rf build/ out/" + "local": "tsc -w", + "test": "tsc && jest build/tests/*", + "clean": "rm -rf build/ out/", + "link": "tsc && mkdir -p out/lib && cp package.json out/ && cp -r build/src/* out/lib/ && cd out/ && npm link", + "prerelease": "rm -rf build/ out/ && tsc && mkdir -p out/lib && cp package.json out/ && cp LICENSE out/ && cp -r build/src/* out/lib/ && npm publish --tag next out/" }, "repository": { "type": "git", diff --git a/src/actions.ts b/src/actions.ts index f63168c..5231f14 100644 --- a/src/actions.ts +++ b/src/actions.ts @@ -18,7 +18,7 @@ const printToConsole: LogAction = (timestamp: Date, level: Levels, name: string, const timeString = `${two(timestamp.getHours())}:${two(timestamp.getMinutes())}:${two(timestamp.getSeconds())}`; console.log('' - + style(`[${timeString}] `, (color.accent ?? 37).toString()) + + style(`[${timeString}] `, (color.accent ?? 39).toString()) + style(`${color.label}`, color.fg.toString(), color.bg.toString()) + style((name ? ` (${name}) ` : ' '), (color.accent ?? 90).toString()) + style(`${message}`, '39', '49') diff --git a/src/config.ts b/src/config.ts index a57a639..a55ade2 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,8 +1,9 @@ import Levels from './levels'; import actions from './actions'; +const globalContext = globalThis as any; const globalKey = 'BARK_LOGGER_CONFIG'; -const env = process?.env ?? {}; +const env = (typeof process === 'object') ? process.env ?? {} : {}; class Config { threshold = env.LOGLEVEL ?? Levels.INFO; @@ -12,8 +13,13 @@ class Config { // Create a singleton instance of config object so that // settings can be shared (and controlled) across all // instances, even between different modules -if (typeof (global as any)[globalKey] === 'undefined') { - (global as any)[globalKey] = new Config(); +if (typeof globalContext[globalKey] === 'undefined') { + Object.defineProperty(globalContext, globalKey, { + value: new Config(), + enumerable: false, + configurable: false, + writable: false + }); } -export default (global as any)[globalKey] as Config; \ No newline at end of file +export default globalContext[globalKey] as Config; \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 6d91839..25da186 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,11 +3,11 @@ import config from './config'; const loggers: { [name: string]: Logger } = {}; -class Logger { +export class Logger { readonly _name: string; - constructor(name: string) { - this._name = name ?? ''; + constructor(name = 'main') { + this._name = name; } trace = (msg: string) => this.log(msg, Levels.TRACE); diff --git a/tests/index.test.ts b/tests/index.test.ts index 0dcb9d4..9e13b2b 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -1,10 +1,10 @@ import { jest, describe, expect, test } from '@jest/globals'; // Import library -import { getLogger, Levels, config } from '../src/index'; +import { Logger, Levels, getLogger, config } from '../src/index'; // Create logger -const logger = getLogger(); +const logger = new Logger(); // Configure mocks const mockAction = jest.fn().mockName('mockAction');