Solved

log4j.properties file

Posted on 2013-11-26
8
305 Views
Last Modified: 2013-12-21
Hi,
In my java spring application i have the following line in log4j.properties file :
log4j.appender.YatraLogs.File=${catalina.base}/logs/fresco.log

When i ran my code on a linux machine it created fresco.log file under the folder :
/data/ytdp/run/tomcat/logs

What i dont understand is how is the value of ${catalina.base} getting picked up ?
There is no environment variable named such.
although there is a environment variable tomcat with value = /data/ytdp/run/tomcat

Thanks
Robin
0
Comment
Question by:Rohit Bajaj
  • 4
  • 3
8 Comments
 
LVL 35

Expert Comment

by:mccarl
ID: 39679461
How does your "tomcat" server get started? Do you start it manually via a shell script, etc? Or is it started automatically from an "/etc/init" style script?

Either way if you look at that script that starts tomcat, you should probably find the answer to your question. If you can't, please post the contents of the script and we can determine what is going on!
0
 

Author Comment

by:Rohit Bajaj
ID: 39691759
HI,
I did find the catalina.base variable set in a script used to start tomcat.
I need to know one thing more.

Here in the log4j.properties file i am using the following parameter to assign the file name :
log4j.appender.YatraLogs.File

This is not the standard parameter as you see the name "YatraLogs" in it.
What i am not getting is how is log4j internally configured to take such input from log4j.properties file.

Also in some cases when i run my code on linux machine with a filename specified in log4j.properties as above.

It doesnt creates the .log file.

Please help me in finding out how to find out the reason and application of using the "log4j.appender.YatraLogs.File" which is different from what log4j expects.
Do i have to look into the log4j.jar file ?? for this


Thanks
0
 
LVL 35

Expert Comment

by:mccarl
ID: 39694205
"YatraLogs" is just a user assigned label. There should be a different line in your config file something like "log4j.appender.YatraLogs=org.apache.log4j.XXXXXXXXXXXX" where the XX's are a class name, perhaps RollingFileAppender or similar.

If the above isn't clear, could you post the entire log4j.properties file and I can probably better explain what is happening and how it all fits together. I do sympathise with you, in that the log4j file format is not the most intuitive thing! :)
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:Rohit Bajaj
ID: 39694684
Hi,
I got what u are saying. Here is the log4j.properties file..

log4j.debug=TRUE

log4j.logger.com.yatra=TRACE, YatraLogs

log4j.logger.org.hibernate=INFO, YatraLogs
log4j.logger.org.springframework=INFO, YatraLogs
log4j.logger.org.apache.velocity=ERROR, YatraLogs
log4j.logger.org.apache.commons=ERROR, YatraLogs
log4j.logger.org.apache.tiles=ERROR, YatraLogs

log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %p %t %c %m %n

log4j.appender.YatraLogs=org.apache.log4j.RollingFileAppender
log4j.appender.YatraLogs.File=/data/ytxdist/SCHEDULER/ExtremeSearch/logs/extremeSearch.log
log4j.appender.YatraLogs.MaxFileSize=10MB
log4j.appender.YatraLogs.MaxBackupIndex=50
log4j.appender.YatraLogs.layout=org.apache.log4j.PatternLayout
log4j.appender.YatraLogs.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %p %t %c %m %n

Open in new window

So i should interpret it like org.apache.log4j.RollingFileAppender.File = /data/ytxdist/SCHEDULER/ExtremeSearch/logs/extremeSearch.log

Here this is the path for the log file dan.
But i dont see it getting created when i run my application on a linux machine.
The path does exists.
Any help on this ? Why would the file not getting created here ?

Thanks for help
0
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 39694731
So i should interpret it like org.apache.log4j.RollingFileAppender.File = /data/ytxdist/SCHEDULER/ExtremeSearch/logs/extremeSearch.log
I guess that that is one way of looking at it. But I would put it like this...

Line 16 is creating an appender called YatraLogs as an instance of org.apache.log4j.RollingFileAppender   And then the foloowing lines are all just setting properties on this instance of the appender.
Lines 3 - 9 are specifying what levels of logging information should be captured from those loggers (ie. the classes in those packages and below) and specifying that the YatraLogs appender should be used.

The main point about the above (and why I differentiate that from what you wrote in your post) is that we are dealing with instances of the RollingFileAppender class, and therefore there could potentially be multiple instances. Take this fictional example...
log4j.debug=TRUE

log4j.logger.com.yatra=TRACE, YatraLogs, YatraLogsAlternate, Console

log4j.logger.org.hibernate=INFO, YatraLogsAlternate
log4j.logger.org.springframework=INFO, YatraLogsAlternate
log4j.logger.org.apache.velocity=ERROR, YatraLogs
log4j.logger.org.apache.commons=ERROR, YatraLogs
log4j.logger.org.apache.tiles=ERROR, Console

log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %p %t %c %m %n

log4j.appender.YatraLogs=org.apache.log4j.RollingFileAppender
log4j.appender.YatraLogs.File=/data/ytxdist/SCHEDULER/ExtremeSearch/logs/extremeSearch.log
log4j.appender.YatraLogs.MaxFileSize=10MB
log4j.appender.YatraLogs.MaxBackupIndex=50
log4j.appender.YatraLogs.layout=org.apache.log4j.PatternLayout
log4j.appender.YatraLogs.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %p %t %c %m %n

log4j.appender.YatraLogsAlternate=org.apache.log4j.RollingFileAppender
log4j.appender.YatraLogsAlternate.File=/test.log
log4j.appender.YatraLogsAlternate.MaxFileSize=5MB
log4j.appender.YatraLogsAlternate.MaxBackupIndex=10
log4j.appender.YatraLogsAlternate.layout=org.apache.log4j.PatternLayout
log4j.appender.YatraLogsAlternate.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %p %t %c %m %n

Open in new window

So with the above we have TWO instances of the RollingFileAppender (in addition to the ConsoleAppender called Console), where the two RollingFileAppender's have different configurations (log filename, max size, etc). It also shows how these "instances" are used for different log messages. So we now have all "com.yatra.*" log messages going to both log files and the console, the Spring and Hibernate message only going to one log file, some of the apache messages going to the other log file and the Tiles messages only going to the console. (As I said, that was just fictional, I doubt you would want that, it's just for explanation purposes)



Now, as for why you aren't getting the log file created, there may be a number of reasons... You say the path exists, but are the permission correct to allow "the user that your application runs as" to create/write the log file? Is your application actually logging anything in those classes that are configured in your properties file?

Unfortunately, it is probably going to be quite hard to debug this further without being there with you. But I can just say to try some different things, play around with it and see what you can find. For example, something like what I gave you above MAY be useful at least for debugging your problem, ie. have all your classes log to the console, the log file that you are after, and maybe a log file in a different location. That way if you get a log message on the console you know it should have gone to the log files, and maybe one log file gets created while the other doesn't, meaning that you may have permission issues, etc.
0
 

Author Comment

by:Rohit Bajaj
ID: 39707861
Hi mccarl,
Thanks for the answer. Can you suggest me some good way to learn java and java spring. Like some books or some open source project i can download online and work on code.

Thanks
0
 
LVL 35

Expert Comment

by:mccarl
ID: 39710341
For Java, I do recommend the Java Tutorials (http://docs.oracle.com/javase/tutorial/). I have given others that link and they made good progress learning the basics.

As for Spring Framework, once you have a good handle on Java, I haven't needed much more than the standard Spring documentation (http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/). Yes, it's not something that you just sit down and read through, cover to cover. But if you start with the "Core technologies" -> "Introduction" and then just move on to the parts that you need, I think that might work.

Personally, I think downloading some existing project and working on the code, might NOT be the best route. The reason being is that you might spend/waste more of your time, just getting your head around the conventions and nuances of that particular developer(s). A better approach might be to think of an idea of your own (a very basic one to start off) and try and code it yourself, using the tutorials/reference doc/occasional google search/EE question, along the way.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SPLUNK REST  API call to Splunk to create and index? 2 70
json example 39 115
JDeveloper 12c for 32 bit 4 35
maven example error 3 36
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now