2121
2222let s: LEVELS = maktaba#log #LEVELS
2323
24+ if ! exists (' s:notification_level' )
25+ let s: notification_level = s: LEVELS .WARN
26+ endif
27+
2428
2529" "
2630" @usage {level} {context} {message} [args...]
@@ -37,6 +41,9 @@ function! s:DoMessage(level, context, message, ...) abort
3741 endif
3842
3943 call s: SendToHandlers ([a: level , localtime (), a: context , l: message ])
44+ if s: notification_level isnot -1 && a: level >= s: notification_level
45+ call s: NotifyMessage (printf (' [%s] %s' , a: context , l: message ), a: level )
46+ endif
4047endfunction
4148
4249
@@ -85,6 +92,34 @@ function! s:SendToHandlers(logitem) abort
8592endfunction
8693
8794
95+ function ! s: NotifyMessage (message, level ) abort
96+ if a: level >= s: LEVELS .ERROR
97+ call maktaba#error#Shout (a: message )
98+ elseif a: level >= s: LEVELS .WARN
99+ call maktaba#error#Warn (a: message )
100+ else
101+ echomsg a: message
102+ endif
103+ endfunction
104+
105+
106+ " "
107+ " Sets the minimum {level} of log messages that will trigger a user
108+ " notification, or -1 to disable notifications. By default, the user will be
109+ " notified after every message logged at WARN or higher.
110+ "
111+ " Notifications will be sent using @function(maktaba#error#Shout) for ERROR
112+ " and SEVERE messages, @function(maktaba#error#Warn) for WARN, and |:echomsg|
113+ " for INFO and DEBUG.
114+ " @throws BadValue
115+ function ! maktaba#log#SetNotificationLevel (level ) abort
116+ call maktaba#ensure#IsTrue (
117+ \ a: level is -1 || maktaba#value#IsIn (a: level , s: LEVELS .Values ()),
118+ \ ' Expected {level} to be maktaba#log#LEVELS value or -1' )
119+ let s: notification_level = a: level
120+ endfunction
121+
122+
88123" "
89124" Gets a string representing a single log {entry}.
90125function ! s: FormatLogEntry (entry) abort
0 commit comments