Log4j 2
  1. Log4j 2
  2. LOG4J2-486

RollingFile Appender - add custom info at the start of each logfile

    Details

    • Type: Question Question
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 2.0-beta9
    • Fix Version/s: 2.0-beta9
    • Component/s: Appenders
    • Labels:
      None
    • Environment:

      Java 1.7, Linux

      Description

      I post this question here because of a hint of Remko Popma.
      See also:

      I want to add some custom info at the top of each logfile, like the version string of my application, the application uptime and the system uptime. And even writing some »bye, bye / eof« to the bottom of the just closed logfile would also be fine.

      Because there is no appropriate hook or callback to get notified when the RollingFileAppander is creating / has created a new file, so that I can put my things at first into these new logfile, I tried to extend DefaultRolloverStrategy. But currently, I stuck at some points.

      Seems that I have to deal with @Plugin and @PluginFactory. My try with the attached log4j2.xml and MyRolloverStrategy.java compiles without errors and warnings. But when I start the application, I get this error message:
      2014-01-05 23:22:05,876 ERROR RollingFile contains an invalid element or attribute "MyRolloverStrategy"

      And then the next step would be: "how to write to the logfiles within my rollover method?"

      log4j2.xml :

      <?xml version="1.0" encoding="UTF-8"?>
      <Configuration>
        <Properties>
          <Property name="projectPrefix">Tts</Property>
          <Property name="rawPattern">%d %-5p [%t] %C{2} (%F:%L) - %m%n</Property>
          <Property name="coloredPattern">%d %highlight{%-5p}{FATAL=bright red, ERROR=red, WARN=yellow, INFO=cyan, DEBUG=green, TRACE=bright blue} %style{[%t] %C{2} (%F:%L) -}{bright,black} %m%n</Property>
          <Property name="coloredShortPattern">%d %highlight{%-5p}{FATAL=bright red, ERROR=red, WARN=yellow, INFO=cyan, DEBUG=green, TRACE=bright blue} %style{[%t] -}{bright,black} %m%n</Property>
          <Property name="fileName">Log/${projectPrefix}.log</Property>
          <Property name="filePattern">Log/${projectPrefix}-%i.log</Property>
        </Properties>
        <Appenders>
          <Console name="Stdout" target="SYSTEM_OUT">
            <PatternLayout pattern="${coloredPattern}"/>
          </Console>
          <RollingFile name="Logfile" fileName="${fileName}" filePattern="${filePattern}">
            <PatternLayout pattern="${rawPattern}"/>
            <Policies>
              <SizeBasedTriggeringPolicy size="16 MB"/>
            </Policies>
            <eeo.toolbox.MyRolloverStrategy fileIndex="min" max="16"/>
          </RollingFile>
        </Appenders>
        <Loggers>
          <Root level="info">
            <AppenderRef ref="Stdout"/>
            <AppenderRef ref="Logfile"/>
          </Root>
        </Loggers>
      </Configuration>
      
      1. MyRolloverStrategy.java
        2 kB
        Joe Merten
      2. log4j2.xml
        1 kB
        Joe Merten

        Issue Links

          Activity

          Joe Merten created issue -
          Joe Merten made changes -
          Field Original Value New Value
          Attachment log4j2.xml [ 12621575 ]
          Attachment MyRolloverStrategy.java [ 12621576 ]
          Joe Merten made changes -
          Description I post this question here because of a hint of Remko Popma.
          See also:
          * http://apache-logging.6191.n7.nabble.com/log4j2-getting-started-amp-rolling-files-tt8406.html#a42402
          * http://stackoverflow.com/questions/20819376/log4j2-rollingfile-appender-add-custom-info-at-the-start-of-each-logfile

          I want to add some custom info at the top of each logfile, like the version string of my application, the application uptime and the system uptime. And even writing some »bye, bye / eof« to the bottom of the just closed logfile would also be fine.

          Because there is no appropriate hook or callback to get notified when the RollingFileAppander is creating / has created a new file, so that I can put my things at first into these new logfile, I tried to extend DefaultRolloverStrategy. But currently, I stuck at some points.

          Seems that I have to deal with {{@Plugin}} and {{@PluginFactory}}. My try with the attached log4j2.xml and MyRolloverStrategy.java compiles without errors and warnings. But when I start the application, I get this error message:
          {{2014-01-05 23:22:05,876 ERROR RollingFile contains an invalid element or attribute "MyRolloverStrategy"}}
          I post this question here because of a hint of Remko Popma.
          See also:
          * http://apache-logging.6191.n7.nabble.com/log4j2-getting-started-amp-rolling-files-tt8406.html#a42402
          * http://stackoverflow.com/questions/20819376/log4j2-rollingfile-appender-add-custom-info-at-the-start-of-each-logfile

          I want to add some custom info at the top of each logfile, like the version string of my application, the application uptime and the system uptime. And even writing some »bye, bye / eof« to the bottom of the just closed logfile would also be fine.

          Because there is no appropriate hook or callback to get notified when the RollingFileAppander is creating / has created a new file, so that I can put my things at first into these new logfile, I tried to extend DefaultRolloverStrategy. But currently, I stuck at some points.

          Seems that I have to deal with {{@Plugin}} and {{@PluginFactory}}. My try with the attached log4j2.xml and MyRolloverStrategy.java compiles without errors and warnings. But when I start the application, I get this error message:
          {{2014-01-05 23:22:05,876 ERROR RollingFile contains an invalid element or attribute "MyRolloverStrategy"}}

          And then the next step would be: "how to write to the logfiles within my rollover method?"
          Remko Popma made changes -
          Description I post this question here because of a hint of Remko Popma.
          See also:
          * http://apache-logging.6191.n7.nabble.com/log4j2-getting-started-amp-rolling-files-tt8406.html#a42402
          * http://stackoverflow.com/questions/20819376/log4j2-rollingfile-appender-add-custom-info-at-the-start-of-each-logfile

          I want to add some custom info at the top of each logfile, like the version string of my application, the application uptime and the system uptime. And even writing some »bye, bye / eof« to the bottom of the just closed logfile would also be fine.

          Because there is no appropriate hook or callback to get notified when the RollingFileAppander is creating / has created a new file, so that I can put my things at first into these new logfile, I tried to extend DefaultRolloverStrategy. But currently, I stuck at some points.

          Seems that I have to deal with {{@Plugin}} and {{@PluginFactory}}. My try with the attached log4j2.xml and MyRolloverStrategy.java compiles without errors and warnings. But when I start the application, I get this error message:
          {{2014-01-05 23:22:05,876 ERROR RollingFile contains an invalid element or attribute "MyRolloverStrategy"}}

          And then the next step would be: "how to write to the logfiles within my rollover method?"
          I post this question here because of a hint of Remko Popma.
          See also:
          * http://apache-logging.6191.n7.nabble.com/log4j2-getting-started-amp-rolling-files-tt8406.html#a42402
          * http://stackoverflow.com/questions/20819376/log4j2-rollingfile-appender-add-custom-info-at-the-start-of-each-logfile

          I want to add some custom info at the top of each logfile, like the version string of my application, the application uptime and the system uptime. And even writing some »bye, bye / eof« to the bottom of the just closed logfile would also be fine.

          Because there is no appropriate hook or callback to get notified when the RollingFileAppander is creating / has created a new file, so that I can put my things at first into these new logfile, I tried to extend DefaultRolloverStrategy. But currently, I stuck at some points.

          Seems that I have to deal with {{@Plugin}} and {{@PluginFactory}}. My try with the attached log4j2.xml and MyRolloverStrategy.java compiles without errors and warnings. But when I start the application, I get this error message:
          {{2014-01-05 23:22:05,876 ERROR RollingFile contains an invalid element or attribute "MyRolloverStrategy"}}

          And then the next step would be: "how to write to the logfiles within my rollover method?"

          log4j2.xml :
          {code}
          <?xml version="1.0" encoding="UTF-8"?>
          <Configuration>
            <Properties>
              <Property name="projectPrefix">Tts</Property>
              <Property name="rawPattern">%d %-5p [%t] %C{2} (%F:%L) - %m%n</Property>
              <Property name="coloredPattern">%d %highlight{%-5p}{FATAL=bright red, ERROR=red, WARN=yellow, INFO=cyan, DEBUG=green, TRACE=bright blue} %style{[%t] %C{2} (%F:%L) -}{bright,black} %m%n</Property>
              <Property name="coloredShortPattern">%d %highlight{%-5p}{FATAL=bright red, ERROR=red, WARN=yellow, INFO=cyan, DEBUG=green, TRACE=bright blue} %style{[%t] -}{bright,black} %m%n</Property>
              <Property name="fileName">Log/${projectPrefix}.log</Property>
              <Property name="filePattern">Log/${projectPrefix}-%i.log</Property>
            </Properties>
            <Appenders>
              <Console name="Stdout" target="SYSTEM_OUT">
                <PatternLayout pattern="${coloredPattern}"/>
              </Console>
              <RollingFile name="Logfile" fileName="${fileName}" filePattern="${filePattern}">
                <PatternLayout pattern="${rawPattern}"/>
                <Policies>
                  <SizeBasedTriggeringPolicy size="16 MB"/>
                </Policies>
                <eeo.toolbox.MyRolloverStrategy fileIndex="min" max="16"/>
              </RollingFile>
            </Appenders>
            <Loggers>
              <Root level="info">
                <AppenderRef ref="Stdout"/>
                <AppenderRef ref="Logfile"/>
              </Root>
            </Loggers>
          </Configuration>
          {code}
          Remko Popma made changes -
          Link This issue relates to LOG4J2-491 [ LOG4J2-491 ]
          Joe Merten made changes -
          Status Open [ 1 ] Resolved [ 5 ]
          Fix Version/s 2.0-beta9 [ 12324756 ]
          Resolution Fixed [ 1 ]
          Joe Merten made changes -
          Comment [ The provided answers let me solve my needs ]
          Ralph Goers made changes -
          Status Resolved [ 5 ] Closed [ 6 ]

            People

            • Assignee:
              Unassigned
              Reporter:
              Joe Merten
            • Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development