• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can Log4j read two properties?

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

... 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
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mea Culpa
Yes it will overwrite,
I think setting threshold for FILE appender to error

and then
will solve it.
Thanks Ulf
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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)?
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
avseq anthoy
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmm..
yes then there is no way but to use two different loggers.
 
avseq anthoy
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic