log4j Properties file on daily basis - java

I have a log4j properties file and i want to generate log file on daily basis and remove the file before today i mean it should contains only today's log file.
I have used following Properties file:
log4j.rootLogger=ALL,Appender2
log4j.appender.Appender2=org.apache.log4j.DailyRollingFileAppender
log4j.appender.Appender2.File=log/AlertLogfile.log
log4j.appender.Appender2.append=true
log4j.appender.Appender2.layout=org.apache.log4j.PatternLayout

From the docs of DailyRollingFileAppender
The rolling schedule is specified by the DatePattern option. This pattern should follow the SimpleDateFormat conventions. In particular, you must escape literal text within a pair of single quotes. A formatted version of the date pattern is used as the suffix for the rolled file name.
log4j.rootLogger=ALL,Appender2
log4j.appender.Appender2=org.apache.log4j.DailyRollingFileAppender
log4j.appender.Appender2.File=log/AlertLogfile.log
log4j.appender.Appender2.append=true
log4j.appender.Appender2.layout=org.apache.log4j.PatternLayout
log4j.appender.Appender2.DatePattern='.'yyyy-MM-dd

Related

How to specify, which resource file to be used while running?

I wants my application to be in both Hindi and English language, so I have created a ResourceBundle named as Resources. In this directory I have create label.properties,label_en_US.properties and label_hi_IN.properties and these file have putted some values like ab=xy_default, ab=xy_in_en and ab=xy_in_hindi respectively.
And Now I planned to use them as label text, so in property of that label i mantioned the default one, like this
Here code is key and In label_defualt is code=Code : , in label_en_US code=Code : and in label_hi_IN code=कोड :. How can I specify which of label.properties to be useout of label, label_en_US or label_hi_in. I have stored user preference in my database like which language to use. I want to know how I can force or tell it use that particular file out of label, label_en_US, label_hi_IN in main function or somewhere else. As now it's taking values from label.properties file only, if user want it to be in hindi then how internally we force to use that label_hi_IN.properties file.
If you've got your files sorted out OK, the right internationalization should occur naturally depending on your locale. If you want to test other supported locales in your software, set the appropriate system properties:
java -Duser.country=IN -Duser.language=hi …
or
java -Duser.country=US -Duser.language=en …
ResourceBundle uses a “fallback” strategy. If the user’s current locale is hi-IN:
ResourceBundle.getBundle will look in label_hi_IN.properties (if that resource exists)
If that file is not found, ResourceBundle.getBundle will look for label_hi.properties (if that resource exists)
If that file is not found, ResourceBundle.getBundle will look for label.properties (if that resource exists)
This means you should do the following:
If label.properties contains English entries, remove label_en_US.properties from your project
If label.properties contains Hindi entries, remove label_hi_IN.properties from your project
Rename label_hi_IN.properties, if it is still present, to label_hi.properties, so all Hindi locales will use that file
Rename label_en_US.properties, if it is still present, to label_en.properties, so all English locales will use that file
Finally I got it, We have to just set Default Locale.
Locale locale = new Locale("hi","IN");
Locale.setDefault(locale);

Log4j rolling file appender: include LAST timestamp in filename

I fiddled around with a rolling file appender with autocompress to .gz, which gave me a lot of files like this:
foo_bar_1.gz
foo_bar_2.gz
...
and so on.
Next I changed the filePattern to "%d{yyyy-MM-dd}_%d{HH-mm-ss}.log.gz" which included a timestamp into the filename. Unfortunately our system management division can't use the timestamp, as this timestamp does not reflect the LAST insertion into the log (or at least the timestamp when rolling and compressing occured).
Is there any way to make Log4j2 to change the filename of the logfile (and of course of the compressed file) to the timestamp of the last insertion?

Log4j to write json array to disk

Im creating a log file system. the log file will be in json format so a server can read it after but i dont think thats too important. What i need to know is can log4j be configured to write into to a file but without any tags like info,debug, timestamp etc in the file. I have looked here
but this polutes the file with with other things. I want ONLY the data i write to show up in the file. I'd also like to set some kind of file rotation on the file if it gets too big after a max size is reached.
This is relatively easy, using a log4j.properties configuration file (place it at the top of your classpath, and Log4j will 'just find it'):
# This is the default logger, simply logs to console
log4j.logger.com.foo.bar=DEBUG,A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
# Note the Pattern here, emits a lot of stuff - btw, don't use this in production
# %C is expensive - see the Javadoc for ConversionPattern for the meaning of all
# the % modifiers:
log4j.appender.A1.layout.ConversionPattern=%d{MMM dd, HH:mm:ss} [%C{2}] %-5p - %m%n
# Logging to file can be enabled by using this one
log4j.logger.com.example=DEBUG, R
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=/var/log/generic.log
log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1
# This is the most minimalist layout you can have: just the 'm'essage is emitted
# (and a \n newline):
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%m%n
All the classes in the com.foo.bar package (and subpackages) will log to console, those in com.example (and below) will log to /var/log/generic.log.
To emit JSON, just use Jackson (com.fasterxml) convert your data to a JSON object and write it out as a string.
What you want is the PatternLayout with %m%n only, and combine with the answer to previously asked question here
You should be able to write a custom log appender, I have a vague recollection of there being an example to do this with MongoDB.
MongoDD stores its data as JSON, so it have converted the log to json format before inserting it.
I would start by searching for a MondgoDB appender, and looking at the file appender that is shipped, that should give you a starting point for an appender if one doesnt already exist.
http://log4mongo.org/display/PUB/Home

Translate the Properties File from one language to another using ResourceBundle

I have message.properties file. by default it will load as English when I use Locale and Resource Bundle in Java file. Now, I need to specify the Country Code or Type in Locale, Then the message.properties file key values should be load as the specified Country in Locale. How can I do this.?
Oracle has some example code of this here.
Basically you create bundles with the correct name for the language of the Localeand then load it with:
ResourceBundle.getBundle("LabelsBundle", currentLocale);
More information about internationalization..
Also, before asking a question try googling for yourself....

How to write comments in JSTL properties files

I'd like to know how to write comments in properties file that is used like this
PF47=Ankomstdatum måste anges innan SIS kan avslutas. # AF PCT
# is supposed to mean a comment but when I render the messages with fmt format JSTL tag output includes the comment. Do comments in properties file have to be on a separate line?
Thank you
It seems as if java-property files only allow comments on separate lines, so you need to write
# AF PCT
PF47=Ankomstdatum måste anges innan SIS kan avslutas.
The format is described in the java.util.Properties documentation. And yes, the # character is only considered as the start of a comment if it's the first non-whitespace character of the line.

Categories

Resources