This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

log4j log rolls but old file deleted automatically

 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My log4j.properties are like

I want to keep up to 5 log files.    But there is no MyLog.log1, MyLog.log2 generated when the size exceeds 5kb,  although I set MaxBackupIndex=5.  It just wipes out MyLog.log and start the same file from empty, and so on.  So I always only have one file MyLog.log there. Whenever it reaches 5kB, it becomes empty and restart filling.  what could be wrong here ?  Thanks.
 
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried changing the values of these two linesto some other value?
Or maybe removing either/both lines altogether and see if you get the expected results.
This could help you see exactly which each line does.

There could some anti-virus like software on the system running this which could be interfering with this process.
 
Saloon Keeper
Posts: 21121
131
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note: instead of


Say
That works under both Windows and Linux/Unix/OSX - and you never have to worry about missing that extra backslash!

I see 2 other possibilities why rolling isn't working. One is that you don't have a qualifier that determines which generation the rolled logfile belongs to. I'm not sure if you need one, but if you do, and don't give it, then one possible result would be that multiple files could not be created.

The other possibility would be if the application lacked creation or write rights to the logs directory. Or, indeed, since you're using a relative file path, whether the directory being used is even where you think it is. Which is why absolute paths are generally best for this sort of thing. If it seems strange that one log file could be created, but not the others, there are several ways that could happen, including the case where the original logfile was created under a different user account than the regular application runs use.
 
Sheriff
Posts: 6259
167
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:Which is why absolute paths are generally best for this sort of thing.


Help me understand this.  Wouldn't that make the log file system specific?
 
Tim Holloway
Saloon Keeper
Posts: 21121
131
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:

Tim Holloway wrote:Which is why absolute paths are generally best for this sort of thing.


Help me understand this.  Wouldn't that make the log file system specific?



Yes, it would. But you generally don't want to play hide-and-seek with logfiles. You want to absolutely know where they are.

Besides, the "current directory" that's being used as the relative file base isn't always where you expect it. Which is one of the ways I mentioned that the current problem can occur. That's especially true when you're configuring logging for an application container system like a webapp server.

The way to get around that without hard-coding a system-dependent path into the app is to inject the logfile path in from outside the app, which can be done via a command-line option or in the case of a webapp, via JNDI.
 
Linwood Hayes
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:Note: instead of


Say
That works under both Windows and Linux/Unix/OSX - and you never have to worry about missing that extra backslash!

I see 2 other possibilities why rolling isn't working. One is that you don't have a qualifier that determines which generation the rolled logfile belongs to. I'm not sure if you need one, but if you do, and don't give it, then one possible result would be that multiple files could not be created.

The other possibility would be if the application lacked creation or write rights to the logs directory. Or, indeed, since you're using a relative file path, whether the directory being used is even where you think it is. Which is why absolute paths are generally best for this sort of thing. If it seems strange that one log file could be created, but not the others, there are several ways that could happen, including the case where the original logfile was created under a different user account than the regular application runs use.



1. could you elaborate " One is that you don't have a qualifier that determines which generation the rolled logfile belongs to. ".  I don't quite understand it, sorry.

2. for the 2nd possibility, I am running this same app, so if it can generate the "MyLog.log", why it can't generate the backup file ?  If it doesn't have the creation/write permission, how could MyLog.log be created ?  Not sure if I get it.

Thanks
 
Tim Holloway
Saloon Keeper
Posts: 21121
131
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Qualifier. A template value so that the logger knows how to create new logfile names - log001, log002, log003... or errorlog-2018030-143303.log, syserrors-G0009V00 or whatever

I, of course, would never, say, manually launch Tomcat from the command line as a root user to test something instead of doing it from the subsystem interface where it would run as userid "tomcat". But if I did, a later proper run might have problems because the root user would create logfiles (and other stuff) as property of the root user and code running as the tomcat user would have problems deleting it, moving it, or even writing it.

Conversely, I might - but of course I'd never be that sloppy - test using the root account and then put into production not realizing that I'd set up my log directories with root privileges, with maybe a few files left over from that test, but no ability to create new files under the tomcat user ID.

Stuff like that.
 
Linwood Hayes
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:Qualifier. A template value so that the logger knows how to create new logfile names - log001, log002, log003... or errorlog-2018030-143303.log, syserrors-G0009V00 or whatever

Stuff like that.



I see what you mean.  But when I googled and learned log4j RollingFileAppender example, I never see anybody explicitly specify such rolled over log file name format.  And it seems log4j just auto append a ".1" ".2" at the end of the file.   Do you have any example of showing how to specify such qualifier ?
 
Linwood Hayes
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Pete Letkeman wrote:Have you tried changing the values of these two linesto some other value?
Or maybe removing either/both lines altogether and see if you get the expected results.
This could help you see exactly which each line does.

There could some anti-virus like software on the system running this which could be interfering with this process.



tried.  it does not help at all.
 
Saloon Keeper
Posts: 2722
354
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know this will not directly help solve your problem, but with log4j2, you can define the filePattern to use for the archive files, and the archive files can be automatically compressed.  I would assume log4j has a similar concepts - I can help more after I get home tonight.

 
Tim Holloway
Saloon Keeper
Posts: 21121
131
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ron's example is a lot like what I do.
 
Linwood Hayes
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.  But I am uing log4j 1.2.x, log4j 2.x uses xml format instead of properties file.
 
Ron McLeod
Saloon Keeper
Posts: 2722
354
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Linwood Hayes wrote:Thanks.  But I am uing log4j 1.2.x, log4j 2.x uses xml format instead of properties file.


Understood - that is why I said "I know this will not directly help solve your problem", but I would expect that the example would provide some clues on how to accomplish something similar with log4j.

Also log4j can also work with XML-based configuration, and I think that for some types of configurations are only possible with XML and not with properties.
 
Ron McLeod
Saloon Keeper
Posts: 2722
354
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a working log4j.xml reference file which will roll base on size.  To use the features in this configuration, log4j-extras is required in addition to the log4 jar file.

This is a simple app to test the rolling.  When you run it, you will need to provide the logging directory in a logdir system property.
For example: java -Dlogdir=c:/temp -jar RollTest.jar

This is an sample of the logging files created: the active file, plus 5 archive files - each 5kB in size.
 
Linwood Hayes
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ron.

1) could you share what log4j version and apache-extra log4j version you used in last code ?

2) <log4j:configuration debug="false">  what is  this debug="false" for ?  I saw you have root level specified inside and each code can have its own logging level defined inside the xml.  Why do you need this top level "debug=false" ?
 
Linwood Hayes
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, does anybody know what log4j 1.2 version starts supporting log4j.xml format ?  I am using log4j 1.2.13, not sure if it supports.
 
Ron McLeod
Saloon Keeper
Posts: 2722
354
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Linwood Hayes wrote:could you share what log4j version and apache-extra log4j version you used in last code ?


log4j.jar
Implementation-Title: log4j
Implementation-Vendor: "Apache Software Foundation"
Implementation-Version: 1.2.16

apache-log4j-extras-1.0.jar
Implementation-Title: Apache Extras Companion for log4j 1.2.
Implementation-Vendor: Apache Software Foundation
Implementation-Version: 1.0


Linwood Hayes wrote:<log4j:configuration debug="false">  what is  this debug="false" for ?


It is used to enable/disable debugging for the log4j implementation (it can also be enabled by setting a system property: -Dlog4j.debug="").  Give it a try - when it is on, you would see something like:

 
Ron McLeod
Saloon Keeper
Posts: 2722
354
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Linwood Hayes wrote:Also, does anybody know what log4j 1.2 version starts supporting log4j.xml format ?  I am using log4j 1.2.13, not sure if it supports.


I tried the example config with version 1.2.13 and it does complain:
I think you need version 1.2.15 or later to use the extras features.
 
Tim Holloway
Saloon Keeper
Posts: 21121
131
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
log4j2 has been out for quite a long time now. As long as you're mucking around with basic log4 infrastructure anyhow, I recommend migrating while you have the time* and can fold the cost into this project instead of having to open a whole new project (with associated learning curve) in a panic when something critical breaks at a - thank you Murphy! - critical time.

Yes, Rob, there are some log4j features that are only supported via XML configuration and I think that some of them may even be related to the rollong logfile appender.

Log4j didn't switch from properties files to XML, it made both available, and as far as I know, if you include one of each, it will apply first the one, then the other.

Linwood Hayes wrote:I tried the example config with version 1.2.13 and it does complain



That is for the same reason that systems like Ant complain. The log4j XML configuration parser is a validating parser and those elements are not defined by an XML schema. If the version of log4j you're using actually did not support XML, it would likely not even know to read the XML config file, much less complain about it.

The XML schema problem is because log4j, like Ant, allows creation of custom components with associated custom config XML elements. Which, because they are custom, could not be anticipated in the main log4 XSD. There are basically 2 ways to shut it up.

1. Create (or find) an XML schema that defines the custom component and add it to the log4j.xml file.

2. Ignore the whining and proceed on.

Although I don't like gratuitous warning and error messages, I'll confess that I've been using solution #2.

---
* Have the time. I make a joke. You're probably already making a panic repair. Because that's how modern business stays "productive".
 
Linwood Hayes
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It works.  Thanks lot Ron and Tim!
 
Linwood Hayes
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more question: In log4j.xml, do I need to create separate <logger> elements for each entry originally in log4j.properties (I am converting log4j.properties to log4j.xml).

log4j.logger.org.apache.struts=WARN
log4j.logger.org.hibernate=WARN

log4j.logger.com.dev1.dev2.dev3=WARN
log4j.logger.com.test1.test2=WARN

Is there a way to simply put multiple things like above into one <logger> element if they have  the same level ?

Thanks.
 
Ron McLeod
Saloon Keeper
Posts: 2722
354
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Linwood Hayes wrote:Is there a way to simply put multiple things like above into one <logger> element if they have  the same level ?


As far as I know, a logger can only have a single name, so you would need something like:

However, if the naming is hierarchical, you could configure a single logger for the parent and its children.
This:
instead of this:

 
Linwood Hayes
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ron.
 
Linwood Hayes
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:

Linwood Hayes wrote:could you share what log4j version and apache-extra log4j version you used in last code ?


log4j.jar
Implementation-Title: log4j
Implementation-Vendor: "Apache Software Foundation"
Implementation-Version: 1.2.16

apache-log4j-extras-1.0.jar
Implementation-Title: Apache Extras Companion for log4j 1.2.
Implementation-Vendor: Apache Software Foundation
Implementation-Version: 1.0


[/code]



Do you think your log4j.xml example will work with log4j 2.x version ?  If I use log4j 2.x what jar would I use ?  Ideally I would like to use this log4j.xml and log4j 2.x version.
 
Linwood Hayes
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Linwood Hayes wrote:

Ron McLeod wrote:

Linwood Hayes wrote:could you share what log4j version and apache-extra log4j version you used in last code ?


log4j.jar
Implementation-Title: log4j
Implementation-Vendor: "Apache Software Foundation"
Implementation-Version: 1.2.16

apache-log4j-extras-1.0.jar
Implementation-Title: Apache Extras Companion for log4j 1.2.
Implementation-Vendor: Apache Software Foundation
Implementation-Version: 1.0


[/code]



Do you think your log4j.xml example will work with log4j 2.x version ?  If I use log4j 2.x what jar would I use ?  Ideally I would like to use this log4j.xml and log4j 2.x version.



Actually I don't think it will work with log4j 2.x  since FixedWindow RollingPloicy class etc are all log4j 1.2.x API not in log4j 2.x version.   Correct me if I am wrong.
 
Ron McLeod
Saloon Keeper
Posts: 2722
354
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Linwood Hayes wrote:Do you think your log4j.xml example will work with log4j 2.x version ?

No, but the log4j2 example that I posted earlier will.

Linwood Hayes wrote:If I use log4j 2.x what jar would I use ?

If you are working with a current version of Java, then download the latest distirbution from the Apache Log4j 2 download page.  If you are working with Java 6, you will need to use an earlier version.
 
Linwood Hayes
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I switched to log4j 2. and I added log4j-api-2.11 jar and log4j-core-2.11 jar (didnt add other jars since I am not using maven).  I have a log4j2.xml but somehow it doesn't work



don't worry about the "logs/mylog" in fileName.  It should be fine since I used same way when I used log4j.xml example and it worked perfectly.  Other than that, can someone see any problem with this log4j2.xml ?  Thanks.
 
Ron McLeod
Saloon Keeper
Posts: 2722
354
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like you have an XML problem.  That last / in the Logger element makes it an empty element, but the element does have content, and a closing tag.  Remove it to make your XML document well-formed.

<Loggers>
   <Logger name="com.myapp" ref="my-log" level="debug"/>
       <AppenderRef ref="my-log" level="debug"/>
   </Logger>
   <Root level="debug" additivity="false">
       <AppenderRef ref="my-log" level="debug"/>
   </Root>
</Loggers>
 
Ron McLeod
Saloon Keeper
Posts: 2722
354
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should have seen something like this when you try to run your app:
It doesn't pin-point the error, but it does indicate a parsing problem and indicates the general area.
 
Ron McLeod
Saloon Keeper
Posts: 2722
354
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, Logger doesn't have a ref attribute, only: name, level, and additivity.

From log4j2 schema:
 
Linwood Hayes
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:It looks like you have an XML problem.  That last / in the Logger element makes it an empty element, but the element does have content, and a closing tag.  Remove it to make your XML document well-formed.

<Loggers>
   <Logger name="com.myapp" ref="my-log" level="debug"/>
       <AppenderRef ref="my-log" level="debug"/>
   </Logger>
   <Root level="debug" additivity="false">
       <AppenderRef ref="my-log" level="debug"/>
   </Root>
</Loggers>



Thanks.  But I still got same error:  


In my java code I use


and things compile OK.
 
Ron McLeod
Saloon Keeper
Posts: 2722
354
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post the contents of your fixed log4j2.xml file.
 
Linwood Hayes
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:Please post the contents of your fixed log4j2.xml file.



 
Ron McLeod
Saloon Keeper
Posts: 2722
354
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The configuration looks fine, except that the additivity attribute should  be with the Logger element.
The error that you posted seems to indicate that Velocity is trying to find a class from Avalon logging framework.  Are you using a 1.X version of Velocity?
 
Linwood Hayes
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Ron.  I am using velocity 1.x version.   it worked when I used log4j 1.2.17 version log4j.xml.   Seems some mismatch when using log4j 2.x...   At this point, I think I will use log4j.xml and log4j-1.2.17/apache-extra-log4j 1.2.17 for time being.  Thanks.
 
Yes, my master! Here is the tiny ad you asked for:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!