I'm using Logback in a couple of my applications to log errors to the database. Now in my newest application I created the same logging that a normally use. But now the application doesn't log anything to the database.
I'm using source code to add a db appender to the logger. The code looks like:
Logger logger = (Logger) LoggerFactory.getLogger("defaultLogger");
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
DataSourceConnectionSource source = new DataSourceConnectionSource();
source.setContext(lc);
source.setDataSource(Database.getInstance().getDatasource());
source.start();
DBAppender dbAppender = new DBAppender();
dbAppender.setName("db");
dbAppender.setConnectionSource(source);
dbAppender.setContext(lc);
dbAppender.start();
logger.addAppender(dbAppender);
logger.setLevel(Level.ALL);
logger.setAdditive(true);
logger.error("test");
When I trying the application to use the logback.xml file same behaviour. No logs are written to the database. Can someone help me with this?
Something was wrong with the database. I removed the logging tables and created them again with the script of Logback. Now everything is working just fine.
Related
I use oracle DB in Spring Boot project. I want to set error logger for the database in Spring console. This configuration must be made in application.properties file I think. (This code example shows another oracle db properties). How can I do that?
oracledb.url = <oracle_url>
oracledb.user = <oracle_user>
oracledb.password = <oracle_password>
oracledb.poolname= <oracle_poolname>
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
Use log4j2 in SpringBoot project with test profile,
org.slf4j.LoggerFactory.getLogger("xxx") can get test profile LoggerContext and read configuration from log4j2-test.xml.
But
org.slf4j.LoggerFactory.getILoggerFactory().getLogger("xxx") can only get current LoggerContext and read configuration from log4j2.xml.
LoggerFactory.getLogger("xxx"); // log4j2-test.xml
LoggerFactory.getILoggerFactory().getLogger("xxx"); // log4j2.xml
Is it a bug of log4j2?
I tested LoggerFactory.getILoggerFactory().getLogger("xxx") with Logback, Logback can choose log4j2-test.xml properly.
SpringBoot version: 2.4.5
Add some background to help more people: ParSeq framework prints logs by LoggerFactory.getILoggerFactory().getLogger("xxx").
No, this would not be a bug in Log4j. Log4j knows nothing about Spring profiles and does not incorporate them in its logic of locating a configuration file.
The methods in LoggerFactory that you are calling are static. That means they are implemented by SLF4J. The SLF4J source shows that getLogger("XXX") does
public static Logger getLogger(String name) {
ILoggerFactory iLoggerFactory = getILoggerFactory();
return iLoggerFactory.getLogger(name);
}
which is exactly the same as what you are manually doing in your second call, so I cannot see how there could be any difference between them.
I also doubt that Logback can choose log4j2-test.xml properly since that would be an invalid configuration file for Logback.
It should be a bug of log4j2:
JIRA_LOG4J2-3083
Many answers around with option of .setlevel() option,which isn't working in my case because of the below .
Scenario:
log4j.xml file under resources is kept with ERROR mode while jetty startup.
Code:
private static Logger logger = Logger.getLogger(test.class);
private static final boolean DEBUG = logger.isDebugEnabled();
if(DEBUG){ //DEBUG will be false as the logging level is set to ERROR
logger.debug("This should print,but it will not print as DEBUG will be set as false during startup");
}
Thanks in advance for your suggestions .
2 solutions:
1. Correct one
Change your mode to the desired one. You should not change the logging level at runtime like that. On test platforms, your log level should be DEBUG. On production, INFO.
2. Dirty one
see Dynamically add appender with slf4j and log4j2. It should be your latest solution.
I have a java app that is running on spring boot.
I'm using tika which in turn uses pdfbox.
I'm using logback as my logging implementation with slf4j.
I know that pdfbox uses apache commons logging.
I'm trying to disable the change the logging level to FATAL like so
<logger name="org.apache.pdfbox" level="FATAL"/>
The problem is that it still doesn't change the level.
I've run this with a debugger. I'm inspecting the logger that pdfbox uses and the results are
result = SLF4JLocationAwareLog
name = org.apache.pdfbox.util.PDFStreamEngine
logger.level = null
logger.loggerContext = ch.qos.logback.classic.LoggerContext[default]
By logger context, I understand that it is indeed using logback, but the configs are not present.
I'll answer my own question and hope that someone will find it useful.
The reason that the logger.level was null is because I didn't specify anything, so it got it from the parent logger. The FATAL didn't work because the highest level is not FATAL but ERROR.
http://logback.qos.ch/apidocs/ch/qos/logback/classic/Level.html
When I changed it to error everything worked as expected.
I need to log whether the Spring context was initialized correctly in a log file. If all the beans were wired and loaded correctly, I need to log that, as well as an incorrect initialization...
I have created the appender and the log file, but the problem is that I don't know if there is something in Spring to log those two events and how can I do it.
Finally managed to do this. Added a logger in my log4j.properties for the ContextLoader class, with a level of ERROR. Then attached my new appender to that logger and this way I get all context loading errors in my new log file.
Thanks
G.