1
votes

This is the database schema

create table log4j_messages 
( 
  message  varchar(2000), 
  class    varchar(255), 
  priority varchar(64), 
  log_date timestamp 
);

and below is my log4j properties

log4j.appender.jdbc=org.apache.log4j.jdbc.JDBCAppender 
log4j.appender.jdbc.driver=org.postgresql.Driver
log4j.appender.jdbc.URL=jdbc:postgresql://MYSERVER:5432/logs 
log4j.appender.jdbc.user=logs
log4j.appender.jdbc.password=MYPASS
log4j.appender.jdbc.sql=INSERT INTO log4j_messages (message, class, priority, log_date) values ('%m', '%c', '%p', to_timestamp('%d', 'YYYY-MM-DD HH24:MI:SS,MS'))

No data is being logged to the database. Is there anyway I can find out what the problem is?

EDIT:

I tried adding log4j.rootCategory=DEBUG at the top of the properties file and now I get the following warnings.

log4j:WARN No appenders could be found for logger (myprogram.Main). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

below is the working version of my log4j properties file when writing to files

log4j.rootCategory=DEBUG, R, O

# Stdout
log4j.appender.O=org.apache.log4j.ConsoleAppender

# File
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=log4j.log

# Control the maximum log file size
log4j.appender.R.MaxFileSize=500KB

# Archive log files (one backup file here)
log4j.appender.R.MaxBackupIndex=1

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.O.layout=org.apache.log4j.PatternLayout

log4j.appender.R.layout.ConversionPattern=[%d{ISO8601}]%5p%6.6r[%t]%x - %C.%M(%F:%L) - %m%n
log4j.appender.O.layout.ConversionPattern=[%d{ISO8601}]%5p%6.6r[%t]%x - %C.%M(%F:%L) - %m%n
1
Ensure jdbc jar in path. Are you correct log-level(debug /error?)? Finally - the appender does not log exceptions. logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/jdbc/… - Jayan
Postgresql expects a point not a comma between the seconds and milliseconds. - Eelke
I changed it to a point and still not working. I've also have the jarpath in Eclipse and I also tried putting it in the bin file and the project file. - Arya
could it be because I don't have a log4j.rootCategory at the top of the properties file? when I put one I get errors - Arya
Is the log4j.properties included in the jar? It seems like it cannot find it. - Hiery Nomus

1 Answers

2
votes

You should set the rootCategory to:

log4j.rootCategory=DEBUG, jbdc

log4j.appender.jdbc=org.apache.log4j.jdbc.JDBCAppender 
log4j.appender.jdbc.driver=org.postgresql.Driver
log4j.appender.jdbc.URL=jdbc:postgresql://MYSERVER:5432/logs 
log4j.appender.jdbc.user=logs
log4j.appender.jdbc.password=MYPASS
log4j.appender.jdbc.sql=INSERT INTO log4j_messages (message, class, priority, log_date) values ('%m', '%c', '%p', to_timestamp('%d', 'YYYY-MM-DD HH24:MI:SS,MS'))

Else the loggers will not have an appender.