11import { bold , green , red , white , yellow } from '../lib/picocolors' ;
22
3+ export type Level = 'error' | 'warn' | 'info' ;
4+ export type LevelWithSilent = 'silent' | Level ;
5+
6+ export interface LogOptions {
7+ /**
8+ * Level of logging
9+ * @default 'event'
10+ */
11+ logLevel ?: LevelWithSilent ;
12+ }
13+
314export const prefixes = {
415 error : red ( bold ( '⨯' ) ) ,
516 warn : yellow ( bold ( '⚠' ) ) ,
617 info : white ( bold ( ' ' ) ) ,
718 event : green ( bold ( '✓' ) ) ,
8- } as const ;
19+ } as const satisfies Record < Level | string , string > ;
20+
21+ export const prefixLevels = {
22+ silent : Infinity ,
23+ error : 40 ,
24+ warn : 30 ,
25+ info : 20 ,
26+ event : 10 ,
27+ } as const satisfies Record < keyof typeof prefixes | 'silent' , number > ;
928
1029const suffix = '(next-runtime-env)' ;
1130
@@ -15,7 +34,17 @@ const LOGGING_METHOD = {
1534 error : 'error' ,
1635} as const ;
1736
18- function prefixedLog ( prefixType : keyof typeof prefixes , message : string ) {
37+ function prefixedLog (
38+ prefixType : keyof typeof prefixes ,
39+ message : string ,
40+ options ?: LogOptions ,
41+ ) {
42+ const { logLevel = 'event' } = options || { } ;
43+
44+ if ( prefixLevels [ prefixType ] < prefixLevels [ logLevel ] ) {
45+ return ;
46+ }
47+
1948 const consoleMethod : keyof typeof LOGGING_METHOD =
2049 prefixType in LOGGING_METHOD
2150 ? LOGGING_METHOD [ prefixType as keyof typeof LOGGING_METHOD ]
@@ -27,18 +56,18 @@ function prefixedLog(prefixType: keyof typeof prefixes, message: string) {
2756 console [ consoleMethod ] ( ` ${ prefix } ` , message , suffix ) ;
2857}
2958
30- export function error ( message : string ) {
31- prefixedLog ( 'error' , message ) ;
59+ export function error ( message : string , options ?: LogOptions ) {
60+ prefixedLog ( 'error' , message , options ) ;
3261}
3362
34- export function warn ( message : string ) {
35- prefixedLog ( 'warn' , message ) ;
63+ export function warn ( message : string , options ?: LogOptions ) {
64+ prefixedLog ( 'warn' , message , options ) ;
3665}
3766
38- export function info ( message : string ) {
39- prefixedLog ( 'info' , message ) ;
67+ export function info ( message : string , options ?: LogOptions ) {
68+ prefixedLog ( 'info' , message , options ) ;
4069}
4170
42- export function event ( message : string ) {
43- prefixedLog ( 'event' , message ) ;
71+ export function event ( message : string , options ?: LogOptions ) {
72+ prefixedLog ( 'event' , message , options ) ;
4473}
0 commit comments