Unable to modify appenders or formatters without restarting the application. #268
Replies: 5 comments
-
Using the public interface for Semantic Logger is the best approach: https://github.com/reidmorrison/semantic_logger/blob/master/lib/semantic_logger/semantic_logger.rb#L175
|
Beta Was this translation helpful? Give feedback.
-
I think the problem I was trying to address is the inability to delete or modify an appender once defined. For example, the following works, but isn't obvious or well-documented: SemanticLogger.add_appender io: $stderr
SemanticLogger.appenders.count
#=> 1
SemanticLogger.appenders.pop
SemanticLogger.appenders
#=> [] Thank you for pointing me in the right direction. I didn't realize that #appenders provided Array-like behavior (including the very useful I'm not sure if I understand the gem's internals well enough to implement a public interface for deleting appenders, although I might take a stab at it. Otherwise, would you be willing to accept a pull request against the README to make the process of appender management and deletion more obvious? I'd be more than happy to offer one. |
Beta Was this translation helpful? Give feedback.
-
Todd, I think you may have missed what Reid was saying? Or did I misunderstand you? There is already a public interface on SemanticLogger for deleting an appender. I encountered a similar need, and my approach was to store the appender in a variable and then delete it later. Here is a script that illustrates deleting appenders both ways: #!/usr/bin/env ruby
require 'semantic_logger'
io_appender = SemanticLogger::Appender::IO.new($stdout)
file_appender = SemanticLogger::Appender::File.new('/dev/null')
SemanticLogger.add_appender(appender: io_appender)
SemanticLogger.add_appender(appender: file_appender)
appenders = SemanticLogger.appenders
puts "appenders is a #{appenders.class} and its ancestors are:\n#{appenders.class.ancestors.inspect}.\n\n"
puts "Starting with #{SemanticLogger.appenders.size} appenders:"
puts appenders
puts
puts 'Now deleting the IO appender using the appenders array...'
appenders.delete(io_appender)
puts "The remaining appenders are:"
puts appenders
puts "\nAnd now deleting the remaining File appender using the SemanticLogger.remove_appender:"
SemanticLogger.remove_appender(file_appender)
puts "The remaining appenders are:"
p appenders Output is:
|
Beta Was this translation helpful? Give feedback.
-
Regarding not being able to change a formatter after the appender has been constructed, this may actually be a good thing. Assuming that multiple threads are writing to the log, it would be possible for messages other than the intended ones to have their formats changed. Also, calls to change the formatter from multiple threads might collide, overwriting each other. Having separate loggers for the separate formats, the way you did, may be the best solution. That said, I guess there could be times when the developer, knowing his or her application would not suffer from any of these risks (as in an application with only one logging thread), would still want to modify the formatter. Perhaps a |
Beta Was this translation helpful? Give feedback.
-
@todd-a-jacobs Also, beware of using the |
Beta Was this translation helpful? Give feedback.
-
Environment
Expected Behavior
Actual Behavior
Unable to Modify or Replace an Appender
There doesn't seem to be an obvious interface for changing the output or formatting for an appender defined directly on SemanticLogger, and attempting to change the format results in an error. As an example, if I want to change a particular log line from the color formatter to JSON, I get a logged error (not an exception):
This will reproduce the error:
There aren't any accessible variables to remove, replace, or modify an appender once it's been defined, either:
As a result, the only way I've found to change formatters is to have completely separate loggers defined. For example:
Unable to Modify the Application Name with Named IO Loggers
Furthermore, setting the application when defining the appender this way or even after the fact looks like it works, but doesn't.
Beta Was this translation helpful? Give feedback.
All reactions