Search code examples
javared5logback

java logback: can i attach a custom function whenever a new log is written


I have a red5 application using java language and I use logback for logging.

whenever a log is written using logback i want to send it to the message to the user. is there a way to somehow attach to the logger class to be able to do another command when a log is being sent ?


Solution

  • When Logback (but the idea applies to Log4J as well) decides to log given statement (based on logging levels and filters), he sends the ILoggingEvent to every so called appender attached to a given logger and its parents.

    What you want is to implement your own appender (class implementing Appender or preferably extending AppenderBase) and add it to your logback.xml (simplified):

    <root>
        <appender class="com.example.YourCustomAppender"/>
    </root>
    

    As you can see it is very simple, but before you write your own appender make sure that a similar appender doesn't already exist.