Solved

log4j.properties file

Posted on 2013-11-26
8
322 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
even odd program using while loop 3 60
login jsp example 24 69
Html Table Looping (part 2) 5 28
Link failure 16 36
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

821 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