Skip to content

Commit

Permalink
Merge pull request #18 from alienfast/fix-cycle-global-configs
Browse files Browse the repository at this point in the history
fix build cycle error, move system configs to globalThis
  • Loading branch information
rosskevin authored Feb 11, 2025
2 parents cdb2967 + 7fd75d0 commit ffaaa0a
Show file tree
Hide file tree
Showing 9 changed files with 257 additions and 39 deletions.
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
},
"type": "module",
"scripts": {
"build": "run-s clean build:ide build:each",
"build": "run-s clean build:each",
"build:ide": "echo 'tsc -b' && tsc -b",
"build:each": "lerna exec --stream --parallel -- yarn build",
"build:each": "lerna exec --stream -- yarn build",
"clean": "tsx ./scripts/clean.ts",
"clean:yarn": "tsx ./scripts/clean-yarn.ts",
"reset": "tsx ./scripts/reset.ts",
Expand All @@ -37,7 +37,7 @@
]
},
"devDependencies": {
"@alienfast/eslint-config": "^5.2.5",
"@alienfast/eslint-config": "^5.2.6",
"@alienfast/logger": "workspace:*",
"@alienfast/prettier-config": "^1.0.2",
"@alienfast/tsconfig": "^1.0.4",
Expand All @@ -55,16 +55,16 @@
"@types/rimraf": "^4",
"@vitejs/plugin-react": "^4.3.4",
"auto": "^11.3.0",
"eslint": "^9.19.0",
"eslint": "^9.20.0",
"execa": "^9.5.2",
"husky": "^9.1.7",
"lint-staged": "^15.4.3",
"npm-run-all": "^4.1.5",
"prettier": "^3.4.2",
"prettier": "^3.5.0",
"rimraf": "^6.0.1",
"tsx": "^4.19.2",
"typescript": "^5.7.3",
"typescript-eslint": "^8.23.0",
"typescript-eslint": "^8.24.0",
"vite": "^6.1.0",
"vite-plugin-dts": "^4.5.0",
"vite-tsconfig-paths": "^5.1.4",
Expand Down
4 changes: 2 additions & 2 deletions packages/logger-browser/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@alienfast/logger-browser",
"version": "11.0.33",
"version": "11.0.32",
"type": "module",
"main-types-note": "This is to appease tsc, types will be removed by clean-package. see https://github.com/rosskevin/ts-esm-workspaces/tree/bug-main-required-to-build#workaround ",
"main": "./dist/index.js",
Expand All @@ -19,7 +19,7 @@
"postpack": "clean-package restore -c ../../.clean-package.json"
},
"devDependencies": {
"@alienfast/logger": "^11.0.33",
"@alienfast/logger": "workspace:*",
"clean-package": "^2.2.0",
"vite": "^6.1.0",
"vitest": "^3.0.5"
Expand Down
4 changes: 2 additions & 2 deletions packages/logger-node/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@alienfast/logger-node",
"version": "11.0.33",
"version": "11.0.32",
"type": "module",
"main-types-note": "This is to appease tsc, types will be removed by clean-package. see https://github.com/rosskevin/ts-esm-workspaces/tree/bug-main-required-to-build#workaround ",
"main": "./dist/index.js",
Expand All @@ -22,7 +22,7 @@
"chalk": "^5.4.1"
},
"devDependencies": {
"@alienfast/logger": "^11.0.33",
"@alienfast/logger": "workspace:*",
"clean-package": "^2.2.0",
"vite": "^6.1.0",
"vitest": "^3.0.5"
Expand Down
1 change: 0 additions & 1 deletion packages/logger/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"@types/node": "^22.13.1"
},
"devDependencies": {
"@alienfast/logger-node": "workspace:*",
"clean-package": "^2.2.0",
"vite": "^6.1.0",
"vitest": "^3.0.5"
Expand Down
2 changes: 0 additions & 2 deletions packages/logger/src/Log.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/* eslint-disable @typescript-eslint/no-unsafe-enum-comparison */
/* eslint-disable @typescript-eslint/no-unsafe-argument */
/* eslint-disable no-console */
/* eslint-disable @typescript-eslint/no-unsafe-call */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */

import { jsonify } from './jsonify.js'
import { Level } from './Level.js'
Expand Down
44 changes: 37 additions & 7 deletions packages/logger/src/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,25 @@ import { Log } from './Log.js'
import { objectName } from './objects.js'
import { LevelOrBoolean, toLevel } from './toLevel.js'

export interface LoggerConfig {
/**
* The default threshold for any new Log
*/
defaultThreshold: Level

/**
* The minimum threshold for the system
*/
systemThreshold: Level
}

if (!globalThis.loggerConfig) {
globalThis.loggerConfig = {
defaultThreshold: Level.INFO,
systemThreshold: Level.DEBUG,
}
}

if (!globalThis.logs) {
globalThis.logs = {}
}
Expand All @@ -12,12 +31,24 @@ export class Logger {
/**
* The default threshold for any new Log
*/
public static defaultThreshold: Level = Level.INFO
public static setDefaultThreshold(level: Level) {
globalThis.loggerConfig.defaultThreshold = level
}

public static getDefaultThreshold() {
return globalThis.loggerConfig.defaultThreshold
}

/**
* The minimum threshold for the system
*/
public static systemThreshold: Level = Level.DEBUG
public static setSystemThreshold(level: Level) {
globalThis.loggerConfig.systemThreshold = level
}

public static getSystemThreshold() {
return globalThis.loggerConfig.systemThreshold
}

/**
* Resolve a logger
Expand All @@ -33,14 +64,13 @@ export class Logger {
}
const name = objectName(object)

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
let log: Log | undefined = globalThis.logs[name]
if (!log) {
// need to delay resolution of LogWriter, so pass this in.
log = new Log({
name,
systemThreshold: this.systemThreshold,
threshold: toLevel(threshold, this.defaultThreshold),
systemThreshold: this.getSystemThreshold(),
threshold: toLevel(threshold, this.getDefaultThreshold()),
})
globalThis.logs[name] = log
// console.log(`Log [${name}] set to ${threshold || this.defaultThreshold}`)
Expand All @@ -62,8 +92,8 @@ export class Logger {
console.info('\tglobalThis.logWriter', globalThis.logWriter)
// console.info('\tFORCE_LOG_WRITER', process && process.env && process.env.FORCE_LOG_WRITER)
console.info('\tlocation', import.meta.url)
console.info('\tsystemThreshold', this.systemThreshold)
console.info('\tdefaultThreshold', this.defaultThreshold)
console.info('\tsystemThreshold', this.getSystemThreshold())
console.info('\tdefaultThreshold', this.getDefaultThreshold)
console.info('\tDEBUG is', Level.DEBUG)
console.info('\tINFO is', Level.INFO)
console.info('\tConfigured logs:')
Expand Down
2 changes: 1 addition & 1 deletion packages/logger/src/configureLoggers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function configureLoggers(loggers: LoggersConfig) {
const threshold = loggers[component]
Logger.get(
component as string,
toLevel(threshold as LevelString | Level, Logger.defaultThreshold),
toLevel(threshold as LevelString | Level, Logger.getDefaultThreshold()),
true,
)
}
Expand Down
3 changes: 2 additions & 1 deletion typings/globalThis.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LogWriter } from './LogWriter'
import { Log, LogWriter, LoggerConfig } from '@alienfast/logger'

// Augment the globalThis interface
// @see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-1-8.html#example-6
Expand All @@ -12,4 +12,5 @@ declare global {
// eslint-disable-next-line no-var
var logWriter: LogWriter
var logs: Record<string, Log>
var loggerConfig: LoggerConfig
}
Loading

0 comments on commit ffaaa0a

Please sign in to comment.