log4j Database Appender does not work properly - java

My log4j.properties file look like
log4j.rootLogger = ERROR,sql, Appender1
log4j.logger.com.endeca=ERROR
log4j.logger.com.endeca.itl.web.metrics=ERROR
log4j.appender.sql=org.apache.log4j.jdbc.JDBCAppender
log4j.appender.sql.URL=jdbc:mysql://192.168.70.39:3306/cortex_mcss_ip
log4j.appender.sql.driver=com.mysql.jdbc.Driver
log4j.appender.sql.user=root
log4j.appender.sql.password=123456
log4j.appender.sql.sql=INSERT INTO errorlog(Level,Msg,CreatedDate) VALUES ('%p','%m',now())
log4j.appender.sql.layout=org.apache.log4j.PatternLayout
log4j.appender.Appender1=org.apache.log4j.ConsoleAppender
log4j.appender.Appender1.layout=org.apache.log4j.PatternLayout
log4j.appender.Appender1.layout.ConversionPattern=%-7p %d [%t] %c %x - %m%n
log4j.appender.Appender2=org.apache.log4j.FileAppender
log4j.appender.Appender2.File=D:/Logs/Log4jWebDemo.log
log4j.appender.Appender2.layout=org.apache.log4j.PatternLayout
log4j.appender.Appender2.layout.ConversionPattern=%-7p %d [%t] %c %x - %m%n
when error occurs it works fine on console and file appender,but does not insert into databse anything ?
Note : databse parameters are 100% accurate in every corner.
Any one please help me to solve this...plz...

To directly address your question:
you should debug this appender to see what happens.
Make sure that DB driver (mysql in this case) appears in the classpath of the application.
Make sure that the table / schema exist, because this appender by its own does not create a schema for you
Note that it has a "bufferSize" parameter, so only if the non-stored message count exceeds the buffer, the actual DB request gets performed.
Place a breakpoint on org.apache.log4j.jdbc.JDBCAppender#execute and see how it really executes
Overall observation/side notes, not directly related to your answer but still can be valuable.
This appender is really outdated and is not a really good solution for modern production applications (unless you have a very small number of logs).
This appender doesn't use batch inserts supported by probably all modern RDBMSs.
This appender doesn't use prepared statements.
All-in-all storing logs in RDBMS doesn't really make sense nowadays, logs are meant to be read and analyzed, and RDBMS doesn't offer really handy tools for this (both visual and from the maintenance standpoint: how do delete obsolete messages? Bulk DELETE is very expensive, partitioning? Retention Policies for records are not really supported in many RDBMSs...
So, a much more modern approach is using tools like ElasticSearch + Kibana (+ some log shipper) or even streaming logs to the cloud (like Logz.io)

Related

Logging with specific label in log file for every user

I have web application in Java (JSP + Servlets) and logging with log4j 1.2.17.
When user login on site he take ID. I wanna see all his actions in log file with ID prefix like this:
01.05.2015 11:12:30 INFO [Roy] - login correct
But i don't want to pass ID in every calling method to logging events in that method.
I want to save ID somewhere and take it when need.
What you probably want is a Mapped Diagnostic Context. This effectively gives you a stack of ThreadLocal to put data elements into that you can log later.
In java...
MDC.put('LOGIN_ID', 'Roy');
In your log pattern:
"%d %p [%X{LOGIN_ID}] %m

How to configure logging of insert statements using pgAdmin?

I want to use pgAdmin to configure to log all sql insert statements. But how?
I found the Backend Configuration Editor and enabled log_statement=all. But how can I control the file to that the log statements are written? (eg: c\logs.txt)
log_destination = 'stderr,csvlog'
log_statement = 'all'
#see C:\Program Files\PostgreSQL\9.3\data\pg_log
See the manual. You probably want to set log_directory and log_filename.
You cannot set individual log destinations for particular categories of log line - say, log statements to one file, other logs to another file. This would be a nice feature; if you have time, consider raising the idea on pgsql-hackers and after discussion submitting a suitable patch.
Most people use csvlog, or use text logs with a log_line_prefix set, then filter the logs with simple tools like grep to extract the desired information.
For statement data collection, also have a look at the pg_stat_statements extension.

How to get logs from current thread only in log4j

I want to intercept logs from the specific thread in my application for the certain period of time. I'm using log4j.
The code below does that, but I'm getting logs from others threads too.
WriterAppender appender = new WriterAppender(new PatternLayout("%d{ISO8601} %p - %m%n"), consoleWriter);
appender.setName("STR_APPENDER");
appender.setThreshold(org.apache.log4j.Level.INFO);
Logger.getRootLogger().addAppender(appender);
//Some Logic here (I'm using other classes and jars here and I want to get this logs as well)
Logger.getRootLogger().removeAppender("STR_APPENDER");
I'm aware that Logger.getRootLogger() is not a good solution here, but I don't have any other idea.
You can use MDC.put.
eg
MDC.put("threadName",Thread.currentThread().getName());
Using that you can put thread name.
Using Log4j appender you can print the thread name [%X{threadName} in logs

How to set a default log level in log4j.properties?

As far as I understand it, in log4j.properties you specify what log levels to let through and for single cases you can restrict it further. However, I want to restrict it for all cases and be more unrestrictive in single cases. The way I tried it (see below, by setting the root logger level to error) I only get error messages but how can I express to only use ERROR as a default level, in case nothing else is specified?
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%m%n
log4j.appender.stdout.threshold=DEBUG
log4j.rootLogger=ERROR,stdout
# everything else should be ERROR
org.mypackage=DEBUG
org.verbosepackage=OFF
Solution
I'm sorry, I just noticed, that I forgot the "log4j.logger." in front of the packages. With it, it really works. Thanks everyone!

How to rotate log files based on time rather than size in Log4j?

I use Log4j with the RollingFileAppender to create a log rotation based on size.
How can I configure it to log to each file for a certain amount of time before rotating?
For example, so that each log file contains one hour of logs, rotating at the top of each hour?
I configure Log4j programatically in Java using a Properties object (as opposed to a log4j.properties file)
You probably want to use a DailyRollingFileAppender. To roll them hourly, for example, you'd use a DatePattern of '.'yyyy-MM-dd-HH. For a log4j.properties file:
log4j.appender.myAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.myAppender.DatePattern='.'yyyy-MM-dd-HH
...
Or for your programmatic configuration:
DailyRollingFileAppender appender = new DailyRollingFileAppender();
appender.setDatePattern("'.'yyyy-MM-dd-HH");
Logger root = Logger.getRootLogger();
root.addAppender(appender);
Unfortunately, using a DailyRollingFileAppender means that you can't limit the file size - this could be problematic if you have tons of logs in the given rolled period.
Additionally,
log4j.appender.myAppender=org.apache.log4j.DailyRollingFileAppender
**log4j.appender.myAppender.DatePattern='.'yyyy-MM-dd-HH**
The following list shows all the date patterns which have defined by log4j,
Minutely '.'yyyy-MM-dd-HH-mm application.log.2013-02-28-13-54
Hourly '.'yyyy-MM-dd-HH application.log.2013-02-28-13
Half-daily '.'yyyy-MM-dd-a application.log.2013-02-28-AM app.log.2013-02-28-PM
Daily '.'yyyy-MM-dd application.log.2013-02-28
Weekly '.'yyyy-ww application.log.2013-07 app.log.2013-08
Monthly '.'yyyy-MM application.log.2013-01 app.log.2013-02
The other thing to be careful of with any rolling file appender is to make sure only one JVM access a particular log file at a time. This is because log4j caches the log file size for performance reasons, and your 'rolling' will get wonky if multiple JVMs access the same files.
Use a DailyRollingFileAppender.
In particular, setting its 'datePattern' property to '.'yyyy-MM-dd-HH would cause file to rotate every hour.
You need to use the DailyRollingFileAppender. Despite its misleading name it can be configured in a to run at configurable time periods up to minutes.

Categories

Resources