SNMP Appender for logback - logback

How to create SNMP Appender for logback? There is a doc in logback manual for migrating log4j appender to logback but which doesn't seem to be working for converting log4j SNMPTrapAppender. Has anyone created logback version o SNMP appender?

Unfortunately, there is no SNMP appender for the moment.
The best alternative solution is to write your own appender. This can be done quite easily.
"You can easily write your appender by subclassing AppenderBase. It handles support for filters, status messages and other functionality shared by most appenders. The derived class only needs to implement one method, namely append(Object eventObject)."
Write your own Appender

I have recently created a new logback snmp appender. Please have a look at https://github.com/anilkd/logback-snmp/wiki/logback-SNMP-appender

Related

Micronaut environments and logback profiles

How to write logback.xml for Micronaut, like Spring Boot:
https://www.logicbig.com/tutorials/spring-framework/spring-boot/profile-logback-logging-config.html
I think springProfile doesn't exist in Micronaut.
I have Micronaut environments and I want to use these for example change logging format only per Micronaut environment.
Best regards
Imre
https://github.com/micronaut-projects/micronaut-examples/blob/master/websocket-chat/src/main/resources/logback.xml
Recently, I ported my spring boot application to Micronaut and used the same logback.xml as given above. Hope this helps.

wildfly10 and slf4j and logback do not work

I followed the following configuration for wildfly10:
https://developer.jboss.org/thread/237094
So to be able to use slf4j and logback in my application I disabled the logging subsystem:
<jboss-deployment-structure>
<deployment>
<!-- exclude-subsystem prevents a subsystems deployment unit processors running on a deployment -->
<!-- which gives basically the same effect as removing the subsystem, but it only affects single deployment -->
<exclude-subsystems>
<subsystem name="logging" />
</exclude-subsystems>
</jboss-deployment-structure>
With this configuration my application uses correctly my logback configuration: the problem is that the server does not use anymore its own logging, so the general information are not written anymore to server.log, but they are written to my application appender.
This sounds very strange to me: I tried a lot of other configurations like excluding directly the modules (i.e. org.slf4j and org.slf4j.impl) on my jboss-deployment-structure.xml file but with no effect.

HikariCP Pool makes Logback's insertFromJNDI configuration stop working

I have two Spring MVC applications that share a commons.jar library. This library includes logback logging library (logback 1.2.3 and slf4j 1.7.25) and the logback.xml file.
Both wars include this line in their web.xml file:
<env-entry>
<env-entry-name>applicationName</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>nameOfApplicationA|nameOfApplicationB</env-entry-value>
</env-entry>
Each application generates its own log file including hostname, for example: HOST1-nameOfApplicationA.log. Logback configuration is as follows:
<insertFromJNDI env-entry-name="java:comp/env/applicationName" as="APP_NAME" />
<appender name="ROLLING_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/${HOSTNAME}-${APP_NAME}.log</file>
...
</appender>
Everything was working OK (Spring MVC 4.3.7.RELEASE, Hibernate 4, C3P0 latest), but we decided to upgrade to Hibernate 5.2.10 and change to HikariCP 2.6.1. After that, logback was no longer able to resolve java:comp/env/applicationName:
ERROR in ch.qos.logback.classic.joran.action.InsertFromJNDIAction - [java:comp/env/applicationName] has null or empty value
Resulting in both applications using the same file name HOST1-APP_NAME_IS_UNDEFINED.log.
As we changed at the same time Hibernate and HikariCP we went back to C3P0 to check the root cause, and can confirm that the new version of Hibernate has nothing to do. The change was developed in its own branch so no other change seems to affect (anyway, when returning to C3P0 it works).
I've been doing some tracing in Hikari's and Logback's code but I'm not able to see anything. I'm stuck, no idea of what to look.
Plan B is insert in each war its own logback.xml but I would like to avoid it and understand the problem as it may affect other parts of the application.
Both wars are deployed together in an Apache Tomcat/8.0.38 server. Tried also 8.5.12. It also happens if only one of the wars is deployed alone.
Although I found no solution, #brettw identified the problem (see https://github.com/brettwooldridge/HikariCP/issues/873), and got a workaround.
It seems that because HikariCP depends on slf4j, and HikariCP is also being initialized and registered into JNDI, is that causing Logback to initialize before the <env-entry> entries have registered.
The test made was initalize Hikari datasource with "org.apache.naming.factory.BeanFactory" factory instead of "com.zaxxer.hikari.HikariJNDIFactory". This way it works correctly.

How to check if log4j2 has been configured or not

We're all familiar with this message when you don't provide a configuration for log4j2:
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
How can I check if log4j2 is not yet configured so that I can initialize with a default configuration if needed?
I ended up creating my own ConfigurationFactory and register it with
-Dlog4j.configurationFactory
That way your ConfigurationFactory will know whether it's been invoked yet or not.
On the first call from your app to log4j2 code, log4j will configure itself before the method returns. If no config file is found, log4j will auto-configure with a default configuration which logs only errors to the console. So from your application's point of view there is never a time that log4j is not configured.
One idea is to check if a log4j2.xml file is in the classpath (using Class.getResource), and if it isn't call System.setProperty("log4j.configurationFile", pathToYourConfig). Note that this must be done before the first call to the log4j2 API.
An alternative:
Once you have a LoggerContext you can call
context.setConfigLocation(configLocation) where configLocation is a URI.
That will force a reconfiguration.

How to Configuring Logging in Jetty via config file?

How do I get jetty to turn down the level of logging from the default of INFO?
I'm actually trying to run the default Apache Solr installation, which ships with jetty, but dumps a lot of information to the console, and I'd only like to see warnings.
I don't want to go hack up the code, I just would like to be able to drop a config file somewhere, but I've been googling for a while, and all I find are obsolete methods or programmatic methods.
Thanks!
edit: -D options would be great, too!
Short answer: java -DDEBUG -jar start.jar
Long answer: (taken from http://docs.codehaus.org/display/JETTY/Debugging)
"Jetty has it's own builtin logging facade that can log to stderr or slf4j (which in turn can log to commons logging, log4j, nlog4j and java logging). Jetty logging looks for a slf4j jar on the classpath. If found, slf4j is used to control logging otherwise stderr is used. The org.mortbay.log.Log class is used to coordinate logging and the following system parameters may be used to control logging:"
org.mortbay.log.class: Specify an implementation of org.mortbay.log.Logger to use
DEBUG: If set, debug logs will be produced, else only INFO and WARN logs will be generated
VERBOSE: If set, verbose logging is produced, including ignored exceptions
IGNORED: If set (jetty 6.1.10 and later), ignored exceptions are logged (independent of DEBUG and VERBOSE settings
Here I undestand that by the "system parameters", in the above cited text, they mean "Java system properties".
If you run jetty 6 as a daemon, the logging config file is:
/usr/share/jetty/resources/log4j.properties
(Where /usr/share/jetty is your $jetty.home.) And to turn down the default log level in that log4jproperties file, change the rootLogger entry:
log4j.rootLogger=WARN, stdout
Find the file logging.properties under your JAVA_HOME directory
Change the default global logging level from
.level= INFO
to
.level= WARNING