@@ -223,19 +223,43 @@ defmodule Logger do
223223 @ levels [ :error , :info , :warn , :debug ]
224224
225225 @ metadata :logger_metadata
226+ @ compile { :inline , __metadata__: 0 }
227+
228+ defp __metadata__ do
229+ Process . get ( @ metadata ) || { true , [ ] }
230+ end
226231
227232 @ doc """
228233 Adds the given keyword list to the current process metadata.
229234 """
230235 def metadata ( dict ) do
231- Process . put ( @ metadata , Keyword . merge ( metadata , dict ) )
236+ { enabled , metadata } = __metadata__ ( )
237+ Process . put ( @ metadata , { enabled , Keyword . merge ( metadata , dict ) } )
232238 end
233239
234240 @ doc """
235241 Reads the current process metadata.
236242 """
237243 def metadata ( ) do
238- Process . get ( @ metadata ) || [ ]
244+ __metadata__ ( ) |> elem ( 1 )
245+ end
246+
247+ @ doc """
248+ Enables logging for the current process.
249+
250+ Currently the only accepted process is self().
251+ """
252+ def enable ( pid ) when pid == self ( ) do
253+ Process . put ( @ metadata , { true , metadata ( ) } )
254+ end
255+
256+ @ doc """
257+ Disables logging for the current process.
258+
259+ Currently the only accepted process is self().
260+ """
261+ def disable ( pid ) when pid == self ( ) do
262+ Process . put ( @ metadata , { false , metadata ( ) } )
239263 end
240264
241265 @ doc """
@@ -368,22 +392,27 @@ defmodule Logger do
368392 @ spec log ( level , message | ( ( ) -> message ) , Keyword . t ) ::
369393 :ok | { :error , :noproc } | { :error , term }
370394 def log ( level , chardata , metadata \\ [ ] ) when level in @ levels and is_list ( metadata ) do
371- % { mode: mode , truncate: truncate ,
372- level: min_level , utc_log: utc_log? } = Logger.Config . __data__
373-
374- if compare_levels ( level , min_level ) != :lt do
375- tuple = { Logger , truncate ( chardata , truncate ) , Logger.Utils . timestamp ( utc_log? ) ,
376- [ pid: self ( ) ] ++ metadata ( ) ++ metadata }
377- try do
378- notify ( mode , { level , Process . group_leader ( ) , tuple } )
395+ case __metadata__ ( ) do
396+ { true , pdict } ->
397+ % { mode: mode , truncate: truncate ,
398+ level: min_level , utc_log: utc_log? } = Logger.Config . __data__
399+
400+ if compare_levels ( level , min_level ) != :lt do
401+ tuple = { Logger , truncate ( chardata , truncate ) , Logger.Utils . timestamp ( utc_log? ) ,
402+ [ pid: self ( ) ] ++ metadata ++ pdict }
403+ try do
404+ notify ( mode , { level , Process . group_leader ( ) , tuple } )
405+ :ok
406+ rescue
407+ ArgumentError -> { :error , :noproc }
408+ catch
409+ :exit , reason -> { :error , reason }
410+ end
411+ else
412+ :ok
413+ end
414+ { false , _ } ->
379415 :ok
380- rescue
381- ArgumentError -> { :error , :noproc }
382- catch
383- :exit , reason -> { :error , reason }
384- end
385- else
386- :ok
387416 end
388417 end
389418
0 commit comments