@@ -7,23 +7,26 @@ defmodule Logger.Backends.Console do
77 max_buffer: nil , buffer_size: 0 , buffer: [ ] , ref: nil , output: nil ]
88
99 def init ( :console ) do
10- if Process . whereis ( :user ) do
11- init ( { :user , [ ] } )
10+ config = Application . get_env ( :logger , :console )
11+ device = Keyword . get ( config , :device , :user )
12+
13+ if Process . whereis ( device ) do
14+ { :ok , init ( config , % __MODULE__ { } ) }
1215 else
1316 { :error , :ignore }
1417 end
1518 end
1619
17- def init ( { device , opts } ) do
18- { :ok , configure ( device , opts , % __MODULE__ { } ) }
20+ def init ( { __MODULE__ , opts } ) when is_list ( opts ) do
21+ config = configure_merge ( Application . get_env ( :logger , :console ) , opts )
22+ { :ok , init ( config , % __MODULE__ { } ) }
1923 end
2024
2125 def handle_call ( { :configure , options } , state ) do
22- { :ok , :ok , configure ( state . device , options , state ) }
26+ { :ok , :ok , configure ( options , state ) }
2327 end
2428
25- def handle_event ( { _level , gl , _event } , state )
26- when node ( gl ) != node ( ) do
29+ def handle_event ( { _level , gl , _event } , state ) when node ( gl ) != node ( ) do
2730 { :ok , state }
2831 end
2932
@@ -79,20 +82,20 @@ defmodule Logger.Backends.Console do
7982 Logger . compare_levels ( lvl , min ) != :lt
8083 end
8184
82- defp configure ( device , options , state ) do
83- config =
84- Application . get_env ( :logger , :console , [ ] )
85- |> configure_merge ( options )
86-
87- if device === :user do
88- Application . put_env ( :logger , :console , config )
89- end
85+ defp configure ( options , state ) do
86+ config = configure_merge ( Application . get_env ( :logger , :console ) , options )
87+ Application . put_env ( :logger , :console , config )
88+ init ( config , state )
89+ end
9090
91- format = Logger.Formatter . compile Keyword . get ( config , :format )
92- level = Keyword . get ( config , :level )
93- metadata = Keyword . get ( config , :metadata , [ ] )
94- colors = configure_colors ( config )
91+ defp init ( config , state ) do
92+ level = Keyword . get ( config , :level )
93+ device = Keyword . get ( config , :device , :user )
94+ format = Logger.Formatter . compile Keyword . get ( config , :format )
95+ colors = configure_colors ( config )
96+ metadata = Keyword . get ( config , :metadata , [ ] )
9597 max_buffer = Keyword . get ( config , :max_buffer , 32 )
98+
9699 % { state | format: format , metadata: Enum . reverse ( metadata ) ,
97100 level: level , colors: colors , device: device , max_buffer: max_buffer }
98101 end
@@ -124,12 +127,12 @@ defmodule Logger.Backends.Console do
124127 % { state | buffer: buffer , buffer_size: buffer_size + 1 }
125128 end
126129
127- defp async_io ( :user , output ) do
128- case Process . whereis ( :user ) do
130+ defp async_io ( name , output ) when is_atom ( name ) do
131+ case Process . whereis ( name ) do
129132 device when is_pid ( device ) ->
130133 async_io ( device , output )
131134 nil ->
132- raise "no device registered with the name :user "
135+ raise "no device registered with the name #{ inspect name } "
133136 end
134137 end
135138
0 commit comments