> Can you be more specific about what you're trying to do with log4j, preferably with a code snippet that illustrates why you can't?
> This is what I don't understand, and your failure to answer my questions
> continues to deepen the mystery.
Oops..
> Let's try these questions and hope for some useful feedback:>
> How is it that you are not able to use log4j to log your database changes?
> Please be specific about what does not work, as in what you are trying to
> accomplish and what happens instead.
I havent even tried using log4j for logging database changes ( because
they need to go into a different log file altogether, away from
application logs).
> What is different about your custom component that makes it succeed where
> log4j fails?
Nothing. Just that My understanding of log4j is probably not
exceptional.
> > Can you be more specific about what you're trying to do with log4j, preferably with a code snippet that illustrates why you can't?
This is my log4j.properties file
*****
# Log4j configuration file.
log4j.rootCategory=DEBUG, A1, A2
# Available levels are DEBUG, INFO, WARN, ERROR, FATAL
# # A1 is a ConsoleAppender
#
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%C %n %-5p [%t] - %m%n
#
# A2 is a DailyRollingFileAppender
log4j.appender.A2=org.apache.log4j.DailyRollingFileAppender
log4j.appender.A2.file=C:/logs/logfile.log
log4j.appender.A2.datePattern='.'yyyy-MM-dd
log4j.appender.A2.append=true
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=%C %-5p %d{ISO8601} [%t] -
%m%n
************************
This is the way I use the logging
*******
private static final Category log =
Category.getInstance(test.class.getName());
PropertyConfigurator.configure("log4j.properties");
log.warn("dis is a warning");
log.info("Dis is a INFO");
log.debug("Log4j really works!");
//what level can I use to log db updates? Ex:
log.dbchange( "Table X updated with
// values ABC");
//Hence here is where I can call my file updater, to
update the changes in a log
// file.
********
This is the output in the log file
*****
test WARN 2007-08-09 13:06:04,828 [main] - dis is a warning
test INFO 2007-08-09 13:06:04,890 [main] - Dis is a INFO
test DEBUG 2007-08-09 13:06:04,890 [main] - Log4j really works!
*****
All I know about log4j is, I can set it up to redirect fatals,
warnings and debugs into a log file.
jsut to be clear, I need to log the warnings, errors and fatal
messages in a seperate file.. and database updates in a seperate file.
Regards
Buddha - 09 Aug 2007 11:04 GMT
@Lew: I will still wait to hear your thoughts. I untangled this code.
Here it is for anyone who wants to use it :
***********************
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.*;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.*;
public class DbLog{
/**
This class will append all the dblogs in a file upto the specified
size.
After which the contents will be copied into another file, which
will be name
along with a time stamp. The hencforth updations will continue in
the file name
specified in the properties file
*/
static void DbLogAppend(String appendValue){
System.out.println("Entering the static method in file1");
WriteToFile(appendValue);
}
static void WriteToFile(String appendValue){
try{
java.util.Properties props = new java.util.Properties();
java.net.URL url = ClassLoader.getSystemResource("props.properties");
props.load(url.openStream());
String fileName = (String)props.get("filename");
String fileMaxSize = (String)props.get("fileMaxSize");
Long longFileMaxSize =new Long(Long.parseLong(fileMaxSize));
long longFileMaxSizept = (long)longFileMaxSize;
String nameRename;
java.io.File file;
java.io.File file2;
Calendar c = Calendar.getInstance();
String date1= new String();
Pattern p = Pattern.compile(":");
String s = new String();
Date now = new Date();
long length;
BufferedWriter out = null;
try {
file = new java.io.File(fileName);
// Create file if it does not exist
boolean exist = file.createNewFile();
if (!exist){
System.out.println("File already exists.");
}else{
System.out.println("File created successfully.");
}
length = file.length();
if(length == longFileMaxSizept || length > longFileMaxSizept){
System.out.println("lenght is "+length);
s = DateFormat.getDateTimeInstance().format(now);
// Create a matcher with an input string
Matcher m = p.matcher(s);
StringBuffer sb = new StringBuffer();
boolean result = m.find();
// Loop through and create a new String with the replacements
while(result) {
m.appendReplacement(sb, "_");
result = m.find();
}
// Add the last segment of input to the new String
m.appendTail(sb);
System.out.println(sb.toString());
// File (or directory) with new name
System.out.println("entering here");
file2 = new java.io.File(file+sb.toString());
// Rename file (or directory)
boolean success = file.renameTo(file2);
if (success) {
System.out.println("Successfully renamed !");
boolean success1 = file.delete();
if (success1){
System.out.println("Successfully deleted !");
}
else
System.out.println("Could not be delted!");
// File was not successfully renamed
}
else{
System.out.println("renaming failed");
}
}
out = new BufferedWriter(new FileWriter(file, true));
out.newLine();
/* uncomment this for non-windows files*/
//out.write("\r");
out.write(appendValue);
out.close();
}catch (IOException e) { //end of try
}
catch(Exception e1){
}
finally {
try{
out.close();
}catch(Exception e){
e.printStackTrace();
}
}
}catch(Exception e){
e.printStackTrace();
}
}
}
***********************
The properties file :
**********************
filename=C://dblog
#in bytes
fileMaxSize=1024
************************
To use it :
*********************
DbLog.DbLogAppend("Tested for db updateasd");
*******************
I would appreciate if someone has tips on improving this code.
Rgds
Arne Vajhøj - 11 Aug 2007 03:14 GMT
> I havent even tried using log4j for logging database changes ( because
> they need to go into a different log file altogether, away from
> application logs).
log4j is capable of logging different output to different files.
Arne