Log4j rolling file appender: include LAST timestamp in filename - java

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?

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);

How to get only the name of my PDF file

I'm developing a project for college which consist reading a CSV file and converting that to a PDF file. That part is fine, I have already done that.
In the end I need to show the name of the PDF file without the full path of where it was created. In other words, I just want the to show the name.
I search a lot to see if there is a simple method that show the name like Java has to show only the name of the File like
file.getName();
Whenever you use iText to create a PDF file, your code sets the target which usually is an OutputStream. If you use a FileOutputStream there, you know the file it writes to.
Thus, all you have to do to to show the name of the PDF File is to inspect your own code and check which target it sets.
Use getBaseName in Apache Commons IO.
getBaseName
public static String getBaseName(String filename)
Gets the base name, minus the full path and extension, from a full
filename.
This method will handle a file in either Unix or Windows format. The
text after the last forward or backslash and before the last dot is
returned.
a/b/c.txt --> c
a.txt --> a
a/b/c --> c
a/b/c/ --> ""
The output will be the same irrespective of the machine that the code
is running on.
Parameters:
filename - the filename to query, null returns null
Returns:
the name of the file without the path, or an empty string if none exists. Null bytes inside string will be removed
Source: https://commons.apache.org/proper/commons-io/javadocs/api-2.5/org/apache/commons/io/FilenameUtils.html#getBaseName(java.lang.String)
If you also need the extension, use getExtension. Which would probably always be .pdf, but you know, it's perfectly valid to have a PDF file without the .pdf filename extension. No sane person would do that but it is better to be prepared for insane users.

log4j Properties file on daily basis

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

jobs run with no mappers or reducers

I have written a job using scalding that runs great in local mode. But when I try to execute it in hdfs mode (on the same file), it doesn't do anything. More precisely, the first step has no tasks (mappers nor reducers) and the steps afterwards obviously do nothing.
I tried grepping the logs for exceptions and also wrap my code in try-catch (in scalding the job definition is in the constructor and I also wrapped the run method).
Maybe for some reason cascading decides to ignore the input file? It is an Avro deflate file.
UPDATE:
Digging more, I can see this line:
2014-04-28 04:49:23,954 INFO org.apache.hadoop.mapred.JobInProgress: Input size for job job_201404280448_0001 = 0. Number of splits = 0
In the job xml, the mapred.input.dir property is set to the path to my file.
It looks like JobInProgress is getting its info from mapred.job.split.file which doesn't exists in the job xml file
It turns out that my avro file is named sample.avro.deflate. Avro, 1.7.4, silently ignores any input files that don't end with '.avro'. In 1.7.6, they added a property avro.mapred.ignore.inputs.without.extension

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

Categories

Resources