How to check if log4j2 has been configured or not - configuration

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.

Related

Can log4jdbc be used with spring boot?

I have a Spring boot app I'm trying to add database logging to which is better than
spring.jpa.properties.hibernate.show_sql=true
log4jdbc, from
https://github.com/marcosemiao/log4jdbc
seems to be the most up to date fork around, seems to format nicely, fills in parameters and adds timing, exactly what I want.
But when I configure it as stated in the readme, changing
spring.datasource.url=jdbc:mysql://localhost:3306/coindatabase?useSSL=false
to
spring.datasource.url=jdbc:log4jdbc:mysql://localhost:3306/coindatabase?useSSL=false
something seems to not like my reference to mysql and seems to try to fall back to H2:
Caused by: java.lang.RuntimeException: Driver org.h2.Driver claims to not accept jdbcUrl, jdbc:log4jdbc:mysql://localhost:3306/coindatabase?useSSL=false
at com.zaxxer.hikari.util.DriverDataSource.<init>(DriverDataSource.java:106)
Is there some easy way to make this work together?
log4jdbc for spring boot wrapper:
<groupId>com.integralblue</groupId>
<artifactId>log4jdbc-spring-boot-starter</artifactId>
which seems to pull in the implementation from:
<groupId>org.bgee.log4jdbc-log4j2</groupId>
<artifactId>log4jdbc-log4j2-jdbc4.1</artifactId>
Additional info:
Don't modify the spring.datasource.url property in your Spring Boot application.properties file; leave the URL as previously defined to access your MYSQL instance.
Instead, after grabbing the com.integralblue maven target, simply set the logging level of choice (ex logging.level.jdbc.sqltiming=info) and your previously defined log4j log will have the DB stuff in it.
See here as was well
You need to use this library in your build.gradle:
// https://mvnrepository.com/artifact/com.integralblue/log4jdbc-spring-boot-starter
compile group: 'com.integralblue', name: 'log4jdbc-spring-boot-starter', version: '2.0.0'
If you get the warning:
"Loading class 'com.mysql.jdbc.Driver'. This is deprecated. The new driver class is 'com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary."
you can set the correct Driver yourself via properties:
log4jdbc.drivers=com.mysql.cj.jdbc.Driver
log4jdbc.auto.load.popular.drivers=false
The documentation for configuration can be found on Github

Confluence html macro ssl exception

We are using the html-include macro for displaying content from an https resource. However, if we include the macro the following exception occurs:
Error rendering macro 'html-include' :
com.atlassian.renderer.v2.macro.MacroException:
com.atlassian.confluence.macro.MacroExecutionException:
javax.net.ssl.SSLHandshakeException:
sun.security.validator.ValidatorException: PKIX path building failed:
sun.security.provider.certpath.SunCertPathBuilderException:
unable to find valid certification path to requested target
We don't know what to do for getting this to work. Can you please help us?
Cause
The two instances communicating do not explicitly trust one another, even though they are using the same keystore. The certificate the instances are using must be present in the truststore for server-to-server communication to work.
Resolution
Follow the instructions in this document: Connecting to LDAP or JIRA or Other Services via SSL if you are on JDK 1.5
Follow the instructions in this document: keytool - Key and Certificate Management Tool if you are on JDK 1.6
This will require a restart of all applications that have the parameter added.

Command prompt doesn't show any exceptions or errors for grails application

I have grails application.
If there are any error or exceptions (compile ot run time), then previously cmd prompt shows all errors/exceptions. But from last few days cmd prompt doesn't show any stacktrace.
So I am unable to figure out where is the problem in my code.
Debugging is literally impossible for me.
Any help will be appreciated.
log4j configuration from Config.Groovy
log4j.main = {
error 'org.codehaus.groovy.grails.web.servlet', // controllers
'org.codehaus.groovy.grails.web.pages', // GSP
'org.codehaus.groovy.grails.web.sitemesh', // layouts
'org.codehaus.groovy.grails.web.mapping.filter', // URL mapping
'org.codehaus.groovy.grails.web.mapping', // URL mapping
'org.codehaus.groovy.grails.commons', // core / classloading
'org.codehaus.groovy.grails.plugins', // plugins
'org.codehaus.groovy.grails.orm.hibernate', // hibernate integration
'org.springframework',
'org.hibernate',
'net.sf.ehcache.hibernate'
}
Config.groovy should have this log4j configuration to enable errors and exceptions to be shown on command prompt.
For detailed logging configuration please check these links Grails Logging and Wiki - Grails logging.

how can I be notified when a bundle throws an exception in osgi runtime? (equinox helios)

If a bundle throws an exception in osgi, the stack-trace is printed on the osgi console. I want to be notified if a bundle throws an exception. I thought using osgi logging service could help about it. However I could not get it to work under Helios.
The question is how can I be notified if a bundle throws an exception in osgi Helios.
Or if osgi logging does the work, how can I get osgi logging to work in Helios? As much as I googled, there is apparently no implementation of osgi loggin service currently integrated in helios. I downloaded the equinox skd 3.6 from eclipse site that contains the bundle org.eclipse.equinox.log, however I could add it as dependency to my plugins, or install it in the osgi runtime.
Any help is really appreciated.
A bundle can only throw an exception when it is invoked. This nearly always when some other bundle invoked one of your published services, or because the bundle received a callback from the framework such as BundleActivator.start().
In the first case, OSGi has absolutely no way to know that an exception happened! Service invocations are direct method calls between two objects, and are not brokered or proxied by the OSGi framework. Therefore if you want to find out about the exception, you must catch it in the calling code.
In the second case, callbacks happen because some bundle caused them to happen. For example, a bundle will be started because some other bundle called Bundle.start(). In this case, an exception thrown from the BundleActivator.start() method would be wrapped in a BundleException that could be caught by the calling code.
So it's really all down to your code, unless you have some third-party bundles that invoke your services or start/stop your bundles (e.g. a web console, or a shell like GoGo). In this case it's down to the third party code. In most cases they should send messages to the LogService, so you should install the log bundle into your framework.
You said that you couldn't install the log bundle, but you didn't say why it failed, what the error message was etc! This kind if information is important if you want help resolving the problem.
You could try Pax Logging and a custom Log4J appender - Pax Logging provides implementations of the OSGi LogService etc as well as wrappers for common logging frameworks.
Do you want to do this purely to log/notify exceptions, or is there some other reason? An UncaughtExceptionHandler might be what you want if it's a case of managing your own or wrapped code.

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