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/
Related
I have an app in java using log4j as my logging jar, but when I try to debug my app and open the log file I find it short and with only the last lines I logged and the rotation files don't show any of the logs that I have seen before.
Is there something wrong with my configuration file? I have multiple projects pointing to the same log file, Could this be the problem?
Here is my log4j.properties:
log4j.rootLogger=ERROR,logfile
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=/opt/tomcat7/logs/mylogs.log
log4j.appender.logfile.MaxFileSize=500KB
# Keep one backup file
log4j.appender.logfile.MaxBackupIndex=2
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %5p (%F:%L) - %m%n
Thanks
You specified level of root logger to be ERROR, while in most cases there won't be a lot of ERROR messages - maybe one-two per many INFO level messages, while you suppressed them: ERROR level allows only ERROR and FATAL messages to be logged. And it filders out any INFO, WARN, DEBUG, TRACE level. Usually you don't use such setup in development. And in production you usually suppress only some verbose classes/packages, but not everything like in config you posted (meaning here ERROR level applies to every class).
So I suppose you should replace ERROR with INFO in your config.
UPDATE: Also likely log4j simply doesn't see your config file. I encountered this issues several times personally and each time I had a workaround like this:
public class Log4JProperties {
public static void setupLog4j(){
String log4jPathFile = Log4JProperties.class.getResource("/log4j.properties").getFile();
Properties props = new Properties();
try {
InputStream configStream = new FileInputStream(log4jPathFile);
props.load(configStream);
configStream.close();
} catch (IOException e) {
System.out.println("log4j configuration file not found");
}
LogManager.resetConfiguration();
PropertyConfigurator.configure(props);
}
}
This is not necessary when I run code from inside Intellij, but when I run using Maven, logging doesn't work without calling this function in the beginning of the execution.
When you specify MaxBackupIndex=2 it will only keep last 2 backups, if log file fills up quickly old backup files will be overridden.
You might have to increase the MaxFileSize or MaxBackupIndex based on your file logging behaviour…
https://logging.apache.org/log4php/docs/appenders/rolling-file.html
I want to output a log message exactly the way it is done using the bash "logger" command, but in Java using Log4j:
Feb 5 19:35:28 hostname program: mymsg
After trying many different patterns, I cannot reproduce the same output. Any idea how to write a pattern for this?
Thank you,
I am guessing you'll get host name from system variables or environmental variables because there is no default parameter for hostname in log4j. So let's assume you'll get hostname and program name from sys variables. So the pattern would be :
<PatternLayout pattern="%d{MMM d HH:mm:ss} ${sys:user.home} ${sys:program}: %m%n" />
See the reference
I've seen questions about it, but none of it helped me.
I'm using log4j, but as it works fine on console, it doesn't write anything to declared files. What is more, files were created, but nothing is saved in them.
Code:
#default
log4j.rootLogger=ERROR,console
#Console Appender
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%5p] [%t %d{hh:mm:ss}] (%F:%M:%L) %m%n
#Custom assignments
log4j.logger.controller=DEBUG,console
log4j.logger.service=DEBUG,console
log4j.logger.dao=DEBUG,console
#Disable additivity
log4j.additivity.controller=false
log4j.additivity.service=false
log4j.additivity.dao=false
#MyLogger
log4j.logger.classPath.myClass = INFO, CACHE
log4j.appender.CACHE=org.apache.log4j.RollingFileAppender
log4j.appender.CACHE.File = ./logs/cache.log
log4j.appender.CACHE.bufferedIO = false
log4j.appender.CACHE.ImmediateFlush=true
log4j.appender.CACHE.Threshold=info
log4j.appender.CACHE.layout=org.apache.log4j.PatternLayout
log4j.appender.CACHE.layout.ConversionPattern=[%5p] [%t %d{hh:mm:ss}] (%F:%M:%L) %m%n
I'm sure that logger is defined properly, because when additivity=false, logs are not showed in console as expected. And when log4j.logger.myClass = INFO, CACHE, console is added as well, logs are showed in console again. So logger declaration seems fine. So why are they not showed in log file?
I'm using org.apache.log4j.Logger.getLogger(myClass.class).info('msg') syntax to use logger.
org.apache.log4j.Logger.getLogger("classPath.myClass").info('msg') is not working either.
When trying log4j.rootLogger=INFO,console,CACHE also nothing appears in the file.
The logger name is specified wrongly. Please change it as follows
#MyLogger
log4j.logger.MyClass = INFO, CACHE
instead of
#MyLogger
log4j.logger.myClass = INFO, CACHE
MyClass is wrongly denoted as myClass. You can also make the logger instantiation as follows
Logger.getLogger("MyClass").info('msg');
Hope this helps!
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
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.