Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

log4j not printing filename and line number in log file

 
Krishna Chaithanya dhulipala
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And by the way, which version and vendor of Java do you use to compile and to run the application?
 
BV Boose
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ...
 
johnson hong
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Johnson and welcome to Javaranch!
 
billson chew
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This thread is still useful .
Resolved my problem.
 
Deepak Kumar
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??

 
Rishi Garg
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many Thanks Johnson..it's working now..
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic