log4j - StringMatchFilter still logging unmatched logs - java

Hope anyone could help me with this issue, so basically I am using log4j-1.2.17 and sorry, I can't update it to log4j2.
As the title suggests, I need to do the following,
I am trying to create an audit.log file which only logs information that has a certain string element (e.g., USER ACTION) and doesn't log anything else.
I have another operations.log that will log everything but not the one with String USER ACTION
My log4j.properties file looks something like this,
log4j.logger.com.ssl=INFO, Output
log4j.logger.com.ssl=INFO, Audit
log4j.appender.Output=org.apache.log4j.DailyRollingFileAppender
log4j.appender.Output.DatePattern='.'yyyy-MM-dd
log4j.appender.Output.File=/var/log/operational/operations.log
log4j.appender.Output.layout=org.apache.log4j.PatternLayout
log4j.appender.Output.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p [%t] %m (%c)%n
log4j.appender.Output.Threshold=TRACE
log4j.appender.Output.filter.1=org.apache.log4j.varia.StringMatchFilter
log4j.appender.Output.filter.1.StringToMatch=USER ACTION
log4j.appender.Output.filter.1.AcceptOnMatch=false
log4j.appender.Output.filter=org.apache.log4j.varia.DenyAllFilter
log4j.appender.Audit=org.apache.log4j.DailyRollingFileAppender
log4j.appender.Audit.DatePattern='.'yyyy-MM-dd
log4j.appender.Audit.File=/var/log/operational/audit.log
log4j.appender.Audit.Threshold=TRACE
log4j.appender.Audit.layout=org.apache.log4j.PatternLayout
log4j.appender.Audit.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p [%t] %m (%c)%n
log4j.appender.Audit.filter.1=org.apache.log4j.varia.StringMatchFilter
log4j.appender.Audit.filter.1.StringToMatch=USER ACTION
log4j.appender.Audit.filter.1.AcceptOnMatch=true
log4j.appender.Audit.filter=org.apache.log4j.varia.DenyAllFilter
Inside the Java Code, I have something like this,
private static final String AUDIT_LOG_PREFIX = "USER ACTION";
logger.info("Just testing if this appears before the one below it");
logger.info("[" + AUDIT_LOG_PREFIX + "] - User Details Updated by User : "+ r.getUserName());
The log output I get is as follows,
operational.log
2018-11-22 10:55:31 INFO [http-nio-8080-exec-179] Just testing if this appears before the one below it (com.ssl.operational.action.EditDetailsActionBean)
audit.log
2018-11-22 10:58:51 INFO [http-nio-8080-exec-192] Just testing if this appears before the one below it (com.ssl.operational.action.EditDetailsActionBean)
2018-11-22 10:58:51 INFO [http-nio-8080-exec-192] [USER ACTION] - User Details Updated by User : adminuser
So really hoping that audit.log doesn't display the first log entry and just displays the one with USER ACTION string.
Any help or advice will be greatly appreciated, Thank you

Related

Setting rollover strategy in log4j2.properties

Can anybody help me with setting the rolloverstrategy in log4j2.properties ?
I have set it up as -
#Appender
appender.test.type=RollingFile
appender.test.name=test
appender.test.fileName=${logPath}/test.log
appender.test.filePattern = ${logPath}/test-%i.log
appender.test.layout.type=PatternLayout
appender.test.layout.pattern=%d{ISO8601} %-5p [%t] %m%n
appender.test.policies.type=Policies
appender.test.policies.size.type=SizeBasedTriggeringPolicy
appender.test.policies.size.size=1MB
appender.test.strategies.type=Strategies
appender.test.strategies.rollover.type=DefaultRolloverStrategy
appender.test.strategies.rollover.max=5
I wrote this consulting https://logging.apache.org/log4j/2.x/manual/configuration.html#ConfigurationSyntax
I didn't find anything for implementing rollover strategy and what you see above is what I thought would work.But unfortunately and obviously it does not.
Here is the error:
2015-11-09 13:30:42,175 localhost-startStop-1 ERROR Unable to locate plugin for Strategies
There is another section on that page that may be useful:
http://logging.apache.org/log4j/2.x/manual/configuration.html#Properties
It contains an example properties configuration with rollover. Furthermore you can find the supported RollingFileAppender Parameters on its manual page: http://logging.apache.org/log4j/2.x/manual/appenders.html#RollingFileAppender
Try this:
appender.test.strategy.type=DefaultRolloverStrategy
appender.test.strategy.max=5
Let me know if it works and I'll add it to the manual page.

Console output of Eclipse with timestamp

I am using Eclipse for my selenium webdriver scripts.Is there any settings/tools/plugins/process so that i can see console output with timestamps.
I am using application log using log4j framework.But there is a requirement from my client that they need console output with timestamps.I have thought of using printing of time before every command,but its not an effective way of doing so.
Thanks,
Manash
Use:
log4j.appender.ConsoleAppender.layout.ConversionPattern=[%-5p] %d %c - %m%n
You have info on the ConversionPattern's printing performance here: http://www.codejava.net/coding/common-conversion-patterns-for-log4js-patternlayout
Not sure if this answer will be valid, unfamiliar with selenium and I dislike log4j (so I will give an answer that works without it).
The documentation for System can be useful, particularly setErr and setOut. You can change the output streams to streams that attach the timestamp before directing the given string to the console. Or instead of doing System.setOut & System.setErr you write a class ABC with public static printstreams like System. When you call the ABC.out.print or ABC.err.print methods, the class adds the timestamp then directs it to System.out & System.err respectively.
Can you please try the below code. The below code is working fine for me.
log4j.rootLogger=debug, stdout, R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%d{dd/MM/yyyy HH:mm:ss}:
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=applicationlog path
log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d{dd/MM/yyyy HH:mm:ss}:
Thanks,
Sudhansu, Mindfiresolutions
http://www.mindfiresolutions.com/

Log4j: Conditional additivity (or propagate event to root logger based on condition?)

I've very simple task: write all events (warn and above) to main log and for certain logger write debug only messages to second log file (warn and above must be logged to main). Sometimes I wish to see debug messages in main log as well (e.g. log4j.logger.com.test=debug)
Besides I must use log4j properties file syntax. I don't understand if additivity (see my last line) can be conditional in that case or should I use complete different approach for the task (which one?).
What I've for now:
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.EnhancedPatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %-5p [%t] %c:%L - %m%n
log4j.rootLogger=warn, stdout
log4j.appender.file2=org.apache.log4j.RollingFileAppender
log4j.appender.file2.maxFileSize=10240KB
log4j.appender.file2.maxBackupIndex=30
log4j.appender.file2.File=${catalina.home}/logs/test.log
log4j.appender.file2.encoding=UTF-8
log4j.appender.file2.layout=org.apache.log4j.PatternLayout
log4j.appender.file2.layout.ConversionPattern=%d %-5p [%t] %c:%L - %m%n
log4j.logger.org.hibernate = debug, file2
log4j.additivity.org.hibernate = false
log4j.logger.com.test=debug
I would recommend you look at Log4J2 as log4j1.x is no longer actively maintained (and Log4J2 has several other advantages like performance improvements).
A common use case is writing debug and higher (info,warn, error, fatal) to a file for developers and warn and higher (warn, error, fatal) to another file eg for support.
From your description it sounds like you want to restrict output to only debug level, and exclude info, warn, error and fatal-level messages. This is possible but not as straight-forward as the use case above.
You probably need to use filters:
http://logging.apache.org/log4j/2.x/manual/filters.html
Take a look at the filter documentation, I'll see if I can dig up an example.

Best technique to print text to console

I am writing a command line tool that performs a number of tests to our servers and reports an output to screen.
I am currently using log4j to print to screen and to a log file.
However, I was wondering if there was a better technique to manage all the printing from one class instead of having the "print" commands scattered all over my code.
e.g.
logger.info("Connecting to environment: " + envName);
if (cmd.hasOption(OPTION_CHECK_PRIMARY)) {
//print primary leg
String primaryLegName = env.getPrimaryLeg().getLegName();
String message = "is primary";
logger.info(String.format("%s : %-4s %s", envName, primaryLegName, message));
}
This is an example of the output that is now scattered across all my code.
Would it be best to have a Formatter class that handles all the printing?
What is the best approach to create it?
What do you think about something like:
Formatter pf = new PlainFormatter();
...
pf.printEnvironment(envName);
if (cmd.hasOption(OPTION_CHECK_PRIMARY)) {
//print primary leg
String primaryLegName = env.getPrimaryLeg().getLegName();
pf.printPrimary(envName, primaryLegName);
}
Have you tried using log4j.properties to format the text. I think if you are trying to format uniquely for each class, then this is still possible using log4j.properties.
Here is a simple example of log4j.properties
log4j.rootLogger=INFO, A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
You can use Pattern in properties like so:
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
log4j has appenders to redirect log messages to different locations. quote" Multiple appenders exist for the console, files, GUI components, remote socket servers, JMS, NT Event Loggers, and remote UNIX Syslog daemons "unquote

A beginner's log4J question

i configured log4j for basic purpose usin the conversion pattern :-
log4j.appender.rollingFile.layout.ConversionPattern=%p %t %c - %m%n
But now i want to log the class name from which the error came as well as the username(available in session object) as well as the date and time when that event occurs. How do i do this? What changes do i need to make in format string?
Thanks in advance :)
Take a look at the PatternLayout docs for most of what you want.
The headache you face is getting the user name from the session (Log4j can't do this automatically). I would perhaps investigate NDCs or MDCs, and populate these from the session (perhaps in a servlet filter?). They are per-thread, so assuming your user has the same scope then this may help.
To get the class name out you can use %l, but you'll take a bit of performance hit.
To get the username out, you'll need to use a mapped or nested diagnostic context and then specificy %X or %x respectively in the pattern string.
Check the PatternLayout javdocs.

Categories