-
-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve category types #21
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export type CategoryList = Readonly<string[]>; | ||
|
||
export type Category = string | CategoryList; | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import type { Category, CategoryList } from "./category.ts"; | ||
import type { Filter } from "./filter.ts"; | ||
import type { LogLevel } from "./level.ts"; | ||
import type { LogRecord } from "./record.ts"; | ||
|
@@ -20,7 +21,7 @@ export interface Logger { | |
/** | ||
* The category of the logger. It is an array of strings. | ||
*/ | ||
readonly category: readonly string[]; | ||
readonly category: CategoryList; | ||
|
||
/** | ||
* The logger with the supercategory of the current logger. If the current | ||
|
@@ -46,9 +47,7 @@ export interface Logger { | |
* @param subcategory The subcategory. | ||
* @returns The child logger. | ||
*/ | ||
getChild( | ||
subcategory: string | readonly [string] | readonly [string, ...string[]], | ||
): Logger; | ||
getChild(subcategory: Category): Logger; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should not allow empty arrays here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In my fork I even made that if a category is empty string, empty array, null or undefined then it will return current logger instead of new child There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's misleading that the method name is |
||
|
||
/** | ||
* Get a logger with contextual properties. This is useful for | ||
|
@@ -385,7 +384,7 @@ export type LogTemplatePrefix = ( | |
* with a single element. | ||
* @returns The logger. | ||
*/ | ||
export function getLogger(category: string | readonly string[] = []): Logger { | ||
export function getLogger(category: Category = []): Logger { | ||
return LoggerImpl.getLogger(category); | ||
} | ||
|
||
|
@@ -408,12 +407,12 @@ interface GlobalRootLoggerRegistry { | |
export class LoggerImpl implements Logger { | ||
readonly parent: LoggerImpl | null; | ||
readonly children: Record<string, LoggerImpl | WeakRef<LoggerImpl>>; | ||
readonly category: readonly string[]; | ||
readonly category: CategoryList; | ||
readonly sinks: Sink[]; | ||
parentSinks: "inherit" | "override" = "inherit"; | ||
readonly filters: Filter[]; | ||
|
||
static getLogger(category: string | readonly string[] = []): LoggerImpl { | ||
static getLogger(category: Category = []): LoggerImpl { | ||
let rootLogger: LoggerImpl | null = globalRootLoggerSymbol in globalThis | ||
? ((globalThis as GlobalRootLoggerRegistry)[globalRootLoggerSymbol] ?? | ||
null) | ||
|
@@ -425,10 +424,10 @@ export class LoggerImpl implements Logger { | |
} | ||
if (typeof category === "string") return rootLogger.getChild(category); | ||
if (category.length === 0) return rootLogger; | ||
return rootLogger.getChild(category as readonly [string, ...string[]]); | ||
return rootLogger.getChild(category); | ||
} | ||
|
||
private constructor(parent: LoggerImpl | null, category: readonly string[]) { | ||
constructor(parent: LoggerImpl | null, category: CategoryList) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why did you remove the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. was playing with class inheritance. this can be rolled back There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
this.parent = parent; | ||
this.children = {}; | ||
this.category = category; | ||
|
@@ -437,10 +436,7 @@ export class LoggerImpl implements Logger { | |
} | ||
|
||
getChild( | ||
subcategory: | ||
| string | ||
| readonly [string] | ||
| readonly [string, ...(readonly string[])], | ||
subcategory: Category, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should not allow empty arrays here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above |
||
): LoggerImpl { | ||
const name = typeof subcategory === "string" ? subcategory : subcategory[0]; | ||
const childRef = this.children[name]; | ||
|
@@ -456,9 +452,7 @@ export class LoggerImpl implements Logger { | |
if (typeof subcategory === "string" || subcategory.length === 1) { | ||
return child; | ||
} | ||
return child.getChild( | ||
subcategory.slice(1) as [string, ...(readonly string[])], | ||
); | ||
return child.getChild(subcategory.slice(1)); | ||
} | ||
|
||
/** | ||
|
@@ -683,7 +677,7 @@ export class LoggerCtx implements Logger { | |
this.properties = properties; | ||
} | ||
|
||
get category(): readonly string[] { | ||
get category(): CategoryList { | ||
return this.logger.category; | ||
} | ||
|
||
|
@@ -692,7 +686,7 @@ export class LoggerCtx implements Logger { | |
} | ||
|
||
getChild( | ||
subcategory: string | readonly [string] | readonly [string, ...string[]], | ||
subcategory: Category, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should not allow empty arrays here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above |
||
): Logger { | ||
return this.logger.getChild(subcategory).with(this.properties); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { type Category, type CategoryList } from "./category.ts"; | ||
export { | ||
type Config, | ||
ConfigError, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add TSDoc comments for those two types?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done