TwP / logging

A flexible logging library for use in Ruby programs based on the design of Java's log4j library.
https://rubygems.org/gems/logging
MIT License
530 stars 101 forks source link

File appenders gets duplicated #190

Closed attilastrba closed 4 years ago

attilastrba commented 6 years ago

Hi, If I try to add the file appender with the same layout and same file name it get's duplicated: So i have to look up the appender by the logfile name before and remove it:

Logging.appenders.each { |appender| logger.remove_appenders(appender.name) if appender.name == log_file}
Logging.logger['test'].add_appenders(Logging.appenders.file(log_file, level: level, truncate: truncate, layout: layout))

Is this a bug or am I missing something?

TwP commented 5 years ago

Each logger does not need it's own appender. You can add the appender to the root logger, and then all log messages will be appended to that location.

Logging::Logger.root.add_appenders(Logging.appenders.file("global.log"))

logger = Logging::Logger["test"]
logger.info("this message will show up in the 'global.log'")

You can always access the same appender by name

Logging.appenders["global.log"]

That ☝️will return the same file appender.

Apologies for the long delay.