Log4j2 Sample Config

Tags: #<Tag:0x00007f434a9b9210> #<Tag:0x00007f434a9b8ef0> #<Tag:0x00007f434a9b8cc0>

Nicely done, thank you! :slightly_smiling_face:

1 Like

This is excellent. Would you consider wiring this up as a PR so it gets included in the Docs? In particular how you created the new loggers will be very useful.

2 Likes

join the discussion :slight_smile:

1 Like

Hi @Dim,

Would you be able to offer any tips for configuring log4j2 to log to the console? I’m using docker and (for most containers) the GELF driver over to Graylog. Spitting everything out to the console and then parsing in Graylog would be a much better option for me but so far I’ve had no luck. :[

J

Hi @joshua_dunham,

I haven’t configured yet my system to use Graylog, so I can’t offer any help.

There is a GELF Appender for log4j2: https://github.com/graylog-labs/log4j2-gelf but I don’t know if it works properly.
In theory, you could use this to channel your logs directly from log4j2 to Graylog.

Thank you for yours example @Dim

Do you also use custom log entries from rules?
I use this in my rules as mentioned in the docs

logDebug("RULES", "Log message from rule xyz")

I tried to add this message to the standard openhab.log by modifying ...logging.cfg as follows

log4j2.logger.rules.name = smarthome.model.script.RULES
log4j2.logger.rules.level = INFO
log4j2.logger.rules.additivity = false
log4j2.logger.rules.appenderRefs = out
log4j2.logger.rules.appenderRef.out.ref = LOGFILE

Unfortunately without success. I think I have not really understood the log4j2 syntax :wink:
Any idea how to get rule logs working again?

Thanks.
Sebastian

I’ve setup custom logging of Zwave and my configuration looks the same as yours so I think you have the syntax right (other then not knowing if the smarthome.model.scripts.RULES is valid, so would look into that piece). Also, have you edited the file appender section at all and are you seeing any other logs go through (I had a type in the filter I put in the appender and it was blocking any logging from happening).

I have not edited the file appender section and all other logs work properly

I use the default destination (openhab.log)

with the above sample config I get the following:
In my rule:

logInfo("FE01","FE01 Timer rescheduled for " + FE01_TimeOut + " minutes")

In openhab.log

18-Aug-2017 20:33:02.532 [INFO ] [org.eclipse.smarthome.model.script.FE01           ] - FE01 Timer rescheduled for 5 minutes

I haven’t tried to redirect the log from my scripts to a custom logger and appender but this shouldn’t be so hard.
Your logger config looks good. You are using the default “LOGFILE” appender (which is the openhab.log RollingRandomAccessFile).

As per @Dim, the correct rules syntax I’ve used is also logInfo(), not logDebug() With that you shouldn’t have to edit your .cfg file at all unless I’m missing what you are trying to accomplish?

Edit: Now that I glanced at the docs, ‘logDebug()’ would only show up if you set your overall log level to DEBUG, whereas ‘logInfo()’ shows up in the default INFO log level

Ok, that is exactly what I’m trying.
And this FE01 doesn’t need to be defined anywhere in the logging configuration?

No, it’s just the identifier that appears on the log file line. Just switch to ‘logInfo()’ and it should work.

what? :slight_smile: to create a new custom logger and appender to keep your rule log entries?
Then, you shouldn’t use the existing appender (LOGFILE) but rather create a new one.

Define a new logger form smarthome.model.script (that will cover all underneath it, including script.RULES and other names)
Define a new appender as in the example above for the ZWAVE and/or KNX bindings

Correct. I just saw this.
The logger is capturing level = INFO but your rule uses logDebug

Ahhhhh :scream:

Thanks for the hint. That was the original reason to create a custom logger. I want to use logDebug but of course set the wrong log level in the config file.

Unfortunately this seems not to be the solution for my problem :weary:

I tried it with logInfo in the rules and absolutely no modification to the log config just as in your example … but still no rule logs appear …

You will end up with huge log files. I’d only recommend running in Debug mode when something particular is giving you issues.

To confirm, you deleted the changes you had made here to the cfg file:

https://community.openhab.org/t/log4j2-sample-config/33284/7?u=rgerrans

Yes, of course I’m using debug mode just for some trails at the moment. And I also wanted to just set the log level to debug for the rules, not for the whole system.

Yes

Finally,

I got it working :champagne:

What works now is the following

  logDebug("RULES", "--- Rule xyz")
  log4j2.logger.rules.name = org.eclipse.smarthome.model.script.RULES
  log4j2.logger.rules.level = DEBUG

No idea what the real problem was.
Thank you for pushing me to the right direction!

2 Likes

That makes much more sense then.

Congrats. I think the issue earlier was that you were setting your logger to INFO level not DEBUG, so now you got them both operating at the same level.

1 Like

Does anyone know if it’s possible to setup a single binding to log to two different files with different log levels? So far my tests haven’t worked.