@@ -6,7 +6,6 @@ interface LogWritable<T> {
66}
77
88export type LoggerLevel = 'debug' | 'info' | 'warn' | 'error' | 'silent' ; // same as Pino
9- export type LoggerEvent = 'info' | 'warn' | 'error' ;
109
1110export interface LogOptions {
1211 dest : LogWritable < LogMessage > ;
@@ -29,7 +28,7 @@ export const dateTimeFormat = new Intl.DateTimeFormat([], {
2928} ) ;
3029
3130export interface LogMessage {
32- type : string | null ;
31+ label : string | null ;
3332 level : LoggerLevel ;
3433 message : string ;
3534}
@@ -43,11 +42,11 @@ export const levels: Record<LoggerLevel, number> = {
4342} ;
4443
4544/** Full logging API */
46- export function log ( opts : LogOptions , level : LoggerLevel , type : string | null , message : string ) {
45+ export function log ( opts : LogOptions , level : LoggerLevel , label : string | null , message : string ) {
4746 const logLevel = opts . level ;
4847 const dest = opts . dest ;
4948 const event : LogMessage = {
50- type ,
49+ label ,
5150 level,
5251 message,
5352 } ;
@@ -61,18 +60,18 @@ export function log(opts: LogOptions, level: LoggerLevel, type: string | null, m
6160}
6261
6362/** Emit a user-facing message. Useful for UI and other console messages. */
64- export function info ( opts : LogOptions , type : string | null , message : string ) {
65- return log ( opts , 'info' , type , message ) ;
63+ export function info ( opts : LogOptions , label : string | null , message : string ) {
64+ return log ( opts , 'info' , label , message ) ;
6665}
6766
6867/** Emit a warning message. Useful for high-priority messages that aren't necessarily errors. */
69- export function warn ( opts : LogOptions , type : string | null , message : string ) {
70- return log ( opts , 'warn' , type , message ) ;
68+ export function warn ( opts : LogOptions , label : string | null , message : string ) {
69+ return log ( opts , 'warn' , label , message ) ;
7170}
7271
7372/** Emit a error message, Useful when Astro can't recover from some error. */
74- export function error ( opts : LogOptions , type : string | null , message : string ) {
75- return log ( opts , 'error' , type , message ) ;
73+ export function error ( opts : LogOptions , label : string | null , message : string ) {
74+ return log ( opts , 'error' , label , message ) ;
7675}
7776
7877type LogFn = typeof info | typeof warn | typeof error ;
@@ -127,3 +126,53 @@ export function timerMessage(message: string, startTime: number = Date.now()) {
127126 timeDiff < 750 ? `${ Math . round ( timeDiff ) } ms` : `${ ( timeDiff / 1000 ) . toFixed ( 1 ) } s` ;
128127 return `${ message } ${ dim ( timeDisplay ) } ` ;
129128}
129+
130+ export class Logger {
131+ options : LogOptions ;
132+ constructor ( options : LogOptions ) {
133+ this . options = options ;
134+ }
135+
136+ info ( label : string , message : string ) {
137+ info ( this . options , label , message ) ;
138+ }
139+ warn ( label : string , message : string ) {
140+ warn ( this . options , label , message ) ;
141+ }
142+ error ( label : string , message : string ) {
143+ error ( this . options , label , message ) ;
144+ }
145+ debug ( label : string , message : string ) {
146+ debug ( this . options , label , message ) ;
147+ }
148+ }
149+
150+ export class AstroIntegrationLogger {
151+ options : LogOptions ;
152+ label : string ;
153+
154+ constructor ( logging : LogOptions , label : string ) {
155+ this . options = logging ;
156+ this . label = label ;
157+ }
158+
159+ /**
160+ * Creates a new logger instance with a new label, but the same log options.
161+ */
162+ fork ( label : string ) : AstroIntegrationLogger {
163+ return new AstroIntegrationLogger ( this . options , label ) ;
164+ }
165+
166+ info ( message : string ) {
167+ info ( this . options , this . label , message ) ;
168+ }
169+ warn ( message : string ) {
170+ warn ( this . options , this . label , message ) ;
171+ }
172+ error ( message : string ) {
173+ error ( this . options , this . label , message ) ;
174+ }
175+ debug ( message : string ) {
176+ debug ( this . options , this . label , message ) ;
177+ }
178+ }
0 commit comments