aspose file tools*
The moose likes Other Open Source Projects and the fly likes Can Log4j read two properties? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "Can Log4j read two properties?" Watch "Can Log4j read two properties?" New topic
Author

Can Log4j read two properties?

avseq anthoy
Ranch Hand

Joined: Apr 27, 2004
Posts: 104
I can configure log4j.properties in the path when I use log4j to log in a file.

I want to configure two log4.properties , one is for logging to file , and the other is for looging to database.
I don't want to log all information into FILE or Database.
In some occasion , I only want to log in database.
Can log4j runtime read which properties file so that I can choose when to log in file and when to log in database?


My Way,My Pace
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42946
    
  70
You can use two log instances in your code, to which you selectively write. They can still be configured in the same log4j.properties file.
prateek urmaliya
Ranch Hand

Joined: Sep 15, 2006
Posts: 87
You can specify to differnt appender namely and
Now you can use the loggers to specify the level info that you want to login to DB or FILE
let's say for your package xyz you want to log debug level and logs beyond but the file you use is only for checking the error level log
so you can achieve this by writing in your log4j


[ March 24, 2007: Message edited by: prateek urmaliya ]

this message brought to you by .... PIE! .... it's yummy! ;)
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42946
    
  70
... you can achieve this by writing in your log4j




Hmm, are you sure that'll work? log4j.properties is a properties file,
so I would have thought that the second key of the same name overrides the first. Is that not the case?
prateek urmaliya
Ranch Hand

Joined: Sep 15, 2006
Posts: 87
Mea Culpa
Yes it will overwrite,
I think setting threshold for FILE appender to error

and then
will solve it.
Thanks Ulf
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42946
    
  70

will solve it.

Actually, I don't think it will. In that case both appenders will have the same information written to, which is what the original poster didn't want. I think there is no way around using two log instances.
avseq anthoy
Ranch Hand

Joined: Apr 27, 2004
Posts: 104
Thanks for your replying.
It look like that I can't use log4j to log message to different destination.
I will use log4j to log to file , and write a few code by myself to log message to DB like insert a record.
[ March 27, 2007: Message edited by: avseq anthoy ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42946
    
  70
It look like that I can't use log4j to log message to different destination.


What have you tried? Did you configure two log instances (as opposed to two log appenders, which was talked about above)?
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
You can have many appenders configured in only one log4j.properties file and as and when required you can plug different appenders to different loggers.


Rahul Bhattacharjee
LinkedIn - Blog
avseq anthoy
Ranch Hand

Joined: Apr 27, 2004
Posts: 104
My log4j config list as below



I use following code to get log instance in my application.


But log will wirte to console and file simultaneously.
How do I configure and use that sometimes I want these message only write to file but sometimes only write to database?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

What is this "sometimes"? Is it like "On this run of the program I want to log to the database, on the next run I want to log to file"? If that's the case you can just configure log4j with the appropriate configuration file when your program starts up.

Or is it like "Now I want the program to start logging to the database, but I don't want to stop it and restart it"?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42946
    
  70
Avseq,

as I've said before, you need to use two log instances, but what your file shows is two log appenders. Now, you will need two appenders, but unless you also create two instances it's not going to work.
prateek urmaliya
Ranch Hand

Joined: Sep 15, 2006
Posts: 87
Actually, I don't think it will. In that case both appenders will have the same information written to, which is what the original poster didn't want. I think there is no way around using two log instances.


I don't know but it's working for me,

let's say for your package hasta.la.vista.baby you want to log WARN level and logs beyond but the file you use is only for checking the ERROR level log

here is what I am doing ,I am using file appender in both cases
TestLogging.java


log4j file


test1.log


test2.log

[ March 28, 2007: Message edited by: prateek urmaliya ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42946
    
  70
That's just using different log levels; it can't log selectively to one or the other, which I understood the original poster wanted.
prateek urmaliya
Ranch Hand

Joined: Sep 15, 2006
Posts: 87
hmm..
yes then there is no way but to use two different loggers.
avseq anthoy
Ranch Hand

Joined: Apr 27, 2004
Posts: 104
I use log4j to log to file and database separately successfully.
My solution is that when log level is "FATAL" , log to database.If log level is higher than "INFO" , log to file.

I want to know how to use two different log instance. Because log4j will look up log4j.propeties in the path by itself.
If I want to use two log instance , should I configure two properteis and set the propreties path in the code?
But I don't want to set the properties path in the code apparently.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can Log4j read two properties?