This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Other Open Source Projects and the fly likes log4j not printing filename and line number in log file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "log4j not printing filename and line number in log file" Watch "log4j not printing filename and line number in log file" New topic
Author

log4j not printing filename and line number in log file

Krishna Chaithanya dhulipala
Greenhorn

Joined: Oct 16, 2008
Posts: 1
Hi all,
I am facing a problem printing filename and line number in log file using log4j.
When I am try this from eclipse its working perfectly. When I am creating a jar of my standalone application using Ant and try to running it form Jar then it is printing (?:?) in place of filename and line number.

Here is my log4j.xml


the log output
1. Run application from eclipse


2. Run from Jar


I am using Spring IOC. Surpricingly spring related logging priting the filename and line numbers in the console even when I run it from Jar.

I found same problem in one of our forums but did not get solution for it.
Can anybody please help me out.

Thanks,
Krishna
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10240
    
168

The line numbers and filename are available only if you compiled the java files with -g enabled (which by the way is the default). See this for more details (look for the -g option there).


[My Blog] [JavaRanch Journal]
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10240
    
168

And by the way, which version and vendor of Java do you use to compile and to run the application?
BV Boose
Ranch Hand

Joined: Jul 26, 2008
Posts: 33
anybody ever come up with a solution to this problem? I'm running into the same thing. The -g option will add to default debugging info, but the default setting is class and line numbers anyway.
from javac compiler reference
Generate all debugging information, including local variables. By default, only line number and source file information is generated.
saqib sarwar
Ranch Hand

Joined: Mar 30, 2007
Posts: 77
Hi,

i think you should look at PtternLayout Documentation.

i found this there .

F: Used to output the file name where the logging request was issued.

WARNING Generating caller location information is extremely slow. It's use should be avoided unless execution speed is not an issue.

L: Used to output the line number from where the logging request was issued.

WARNING Generating caller location information is extremely slow. It's use should be avoided unless execution speed is not an issue.

SO.

might be its a problem of execution speed.

think about it ...


SCJP5 95%, SCWCD 85%<br />Knowledge is the Life of Mind
johnson hong
Greenhorn

Joined: May 07, 2010
Posts: 1
I guess you must compile the jar with ant,by default,ant do not enable debug,so you can get neither FILENAME nor LINE NUMBER,just enable debug by add debug = true while compiling,for example:
<javac destdir="XXXX" source="1.5" target="1.5" debug="true" encoding="UTF-8">
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

Hi Johnson and welcome to Javaranch!


Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
billson chew
Greenhorn

Joined: Oct 03, 2007
Posts: 4
i'm having this kind of issue is because i'm using org.apache.log4j.AsyncAppender
if i change back to use org.apache.log4j.rolling.RollingFileAppender
it was working perfect and sweet.

if you still insist to use asyncappender
here is the solution i got from: http://marc.info/?l=log4j-user&m=105581147910000&w=2

<appender name="ASYNC" class="org.apache.log4j.AsyncAppender">
<param name="BufferSize" value="256"/>
<param name="LocationInfo" value="true"/>
</appender>

after add in <param name="LocationInfo" value="true"/>
you'll get the filename and linenumber appear correctly.

Krishna Chaithanya dhulipala wrote:Hi all,
I am facing a problem printing filename and line number in log file using log4j.
When I am try this from eclipse its working perfectly. When I am creating a jar of my standalone application using Ant and try to running it form Jar then it is printing (?:?) in place of filename and line number.
Krishna
vicky Aggarwal
Ranch Hand

Joined: May 15, 2009
Posts: 40
This thread is still useful .
Resolved my problem.
Deepak Kumar
Ranch Hand

Joined: Nov 05, 2007
Posts: 62
johnson hong wrote:I guess you must compile the jar with ant,by default,ant do not enable debug,so you can get neither FILENAME nor LINE NUMBER,just enable debug by add debug = true while compiling,for example:
<javac destdir="XXXX" source="1.5" target="1.5" debug="true" encoding="UTF-8">


enabling debug to true worked for me!!!
Thanks Johnson!! , is it because that by setting debug to true the values will be passed to compilers??


Thanks,
Deepak
Rishi Garg
Greenhorn

Joined: Oct 12, 2011
Posts: 1
Many Thanks Johnson..it's working now..
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10240
    
168

Deepak Kumar wrote:
Thanks Johnson!! , is it because that by setting debug to true the values will be passed to compilers??


As per the documentation of Ant javac task

debug - Indicates whether source should be compiled with debug information; defaults to off. If set to off, -g:none will be passed on the command line for compilers that support it (for other compilers, no command line argument will be used). If set to true, the value of the debuglevel attribute determines the command line argument.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: log4j not printing filename and line number in log file