How to change colour of logging output log4j [duplicate] - java

This question already has answers here:
Making a log4j console appender use different colors for different threads
(3 answers)
Closed 9 years ago.
I'm currently working on a Java project and we are using logging. When a Log is made it is always printed in plain black text, whatever the logging level (INFO, ERROR, etc.)
How can I override the colour of these outputs so for example all ERROR logs will be red while all WARN logs will be orange etc.
Thanks
EDIT: I managed to download the ANSIColorLayout file and my log4j.properties now refers to it. However I get the following errors:
log4j:WARN No such property [all] in org.apache.log4j.ConsoleAppender.
log4j:WARN No such property [reset] in org.apache.log4j.ConsoleAppender.
log4j:WARN No such property [stacktrace] in org.apache.log4j.ConsoleAppender.
log4j:WARN No such property [info] in org.apache.log4j.ConsoleAppender.
log4j:WARN No such property [error] in org.apache.log4j.ConsoleAppender.
log4j:WARN No such property [defaultcolor] in org.apache.log4j.ConsoleAppender.
My log4j.properties file looks like this:
log4j.rootLogger = DEBUG, CA, FA
log4j.appender.CA = org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout = balle.logging.ANSIColorLayout
log4j.appender.CA.layout.ConversionPattern=%-5p [%d{MM-dd-yyyy HH:mm:ss}] %c - %m%n
log4j.appender.CA.all=\u001B[1;37m log4j.appender.A1.fatal=\u001B[1;31m
log4j.appender.CA.error=\u001B[0;31m log4j.appender.A1.warn=\u001B[1;33m
log4j.appender.CA.info=\u001B[0;37m log4j.appender.A1.debug=\u001B[0;36m
log4j.appender.CA.reset=\u001B[1;37m
log4j.appender.CA.stacktrace=\u001B[0;31m
log4j.appender.CA.defaultcolor=\u001B[1;37m
My guess is that I'm supposed to use my own custom ConsoleAppender? Does anyone have any ideas?
Thanks

You can download one of the various ANSIColorLayout.java implementation. These "color loggers" work by extending the PatternLayout class.
Then you can simply do something like this in your log4j properties:
log4j.appender.stdout.layout=com.acme.ANSIColorLayout
Here's a link to one ANSIColorLayout.java ready to use:
http://code.google.com/p/a-distributed-file-system/source/browse/trunk/DistributedFileSystem/ui/net/dfs/ui/ANSIColorLayout.java

Related

Tinylog capture System Error output stream to log file

I am using Tinylog for logging everything in my java application. I capture all the exceptions and logs them.
<dependency>
<groupId>org.tinylog</groupId>
<artifactId>tinylog-api</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.tinylog</groupId>
<artifactId>tinylog-impl</artifactId>
<version>2.2.1</version>
</dependency>
I am writing the logs on Console as well as on File. Here is my tinylog.properties file
writer1= console
writer2 = rolling file
writer2.file = /var/log/oozie-monitor-log/monitor-{count}.log
writer2.charset = UTF-8
writer2.append = true
writer2.buffered = false
writer2.backups = 15
#writer1.level = debug
#writer2.level = debug
writer2.policies = daily
writer1.format = {date: HH:mm:ss.SSS} {level}: {message}
writer2.format = {date: HH:mm:ss.SSS} {level}: {message}
Now the issue is that there are some system error and warning that is not getting captured by Tinylog.
For example I see below logs on the Console but these logs are available in log file
log4j:WARN No appenders could be found for logger (org.apache.hadoop.util.Shell).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/Users/neeleshnirmal/.m2/repository/org/slf4j/slf4j-simple/1.6.6/slf4j-simple-1.6.6.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/neeleshnirmal/.m2/repository/org/slf4j/slf4j-log4j12/1.6.6/slf4j-log4j12-1.6.6.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.SimpleLoggerFactory]
So my question is how can configure Tinylog to capture the System error stream also?
tinylog does not provide any adapter for forwarding console output to the logging framework. It is possible to develop such an adapter by just a few lines. However, it won't help you, because it would create an infinite loop in your use case as you use also a console writer for outputting log entries into the console. Therefore, such hypothetical console adapter would also capture console output from the console writer and send again to the console writer.
Instead, I recommend to fix the warnings. Your warning examples are quite easy to fix: Remove slf4j-simple-1.6.6.jar, slf4j-log4j12-1.6.6.jar, and the log4j JAR from the classpath. Afterwards you can add slf4j-tinylog and log4j1.2-api. This will not only fix the warnings, but also forward all logging entries from SLF4J and Log4j to tinylog.

properties file exists, but still getting "WARN No appenders could be found"

I'm using the pcap4j library in my application and when I run it I get:
log4j:WARN No appenders could be found for logger (org.pcap4j.core.NativeMappings).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
I also use log4j in my application:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.1</version>
</dependency>
which is configured in main/resources/log4j2-test.properties:
log4j.rootLogger=INFO, fileLogger
log4j.appender.fileLogger.layout=org.apache.log4j.PatternLayout
log4j.appender.fileLogger.layout.ConversionPattern=%d [%t] %-5p (%F:%L) - %m%n
log4j.appender.fileLogger.File=application.log
log4j.appender.fileLogger=org.apache.log4j.DailyRollingFileAppender
log4j.appender.fileLogger.datePattern='.'yyyy-MM-dd-HH-mm
So the configuration file is there and whatever I change in it it has an effect which means log4j sees it and it works, so I'm quite helpless when I read everywhere that this kind of warning is caused by a missing or wrong config file.
Can it be that the error is in the dependency and I can't do anything about it? If so, is there a way to disable the logging in this dependency completely? Actually this is that I would want in the first place anyway.
That message is from log4j 1, whereas you are using log4j2, therefore it is harmless.
https://github.com/apache/log4j/blob/trunk/src/main/java/org/apache/log4j/Hierarchy.java
public
void emitNoAppenderWarning(Category cat) {
// No appenders in hierarchy, warn user only once.
if(!this.emittedNoAppenderWarning) {
LogLog.warn("No appenders could be found for logger (" +
cat.getName() + ").");
LogLog.warn("Please initialize the log4j system properly.");
LogLog.warn("See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.");
this.emittedNoAppenderWarning = true;
}
}
Off topic, sorry :)
You can manually set the log config file using this :
org.apache.log4j.xml.DOMConfigurator.configure("<path>\log4j2-test.properties");
The <path> can either be absolute or relative to the root of your maven module.

Install Esper in Eclipse

I'm struggling to install Esper in Eclipse. I'm not good at technical issues, so please I need all details to do so.
In fact, I added all jar files of esper-5.4.0.zip in Java build path of my project. I have got the following error messages:
log4j:WARN No appenders could be found for logger (com.espertech.esper.util.ObjectInputStreamWithTCCL).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" com.espertech.esper.client.EPStatementException: Failed to resolve event type: Event type or class named 'Deposit' was not found [every A=Deposit]
at com.espertech.esper.core.service.StatementLifecycleSvcImpl.compile(StatementLifecycleSvcImpl.java:1162)
at com.espertech.esper.core.service.StatementLifecycleSvcImpl.createStopped(StatementLifecycleSvcImpl.java:298)
at com.espertech.esper.core.service.StatementLifecycleSvcImpl.createStoppedAssignName(StatementLifecycleSvcImpl.java:202)
at com.espertech.esper.core.service.StatementLifecycleSvcImpl.createAndStart(StatementLifecycleSvcImpl.java:156)
at com.espertech.esper.core.service.EPAdministratorImpl.createPatternStmt(EPAdministratorImpl.java:108)
at com.espertech.esper.core.service.EPAdministratorImpl.createPattern(EPAdministratorImpl.java:58)
at test.CEP_start.main(CEP_start.java:39)
When you create a statement like select * from Deposit that requires that you tell the engine what a "Deposit" is. That is done by adding an event type.
The documentation and tutorials have plenty of introductory examples or you could review the examples that comes with the Esper download.

Initializing log4j for a stand alone application

I'm a newbie to log4j. This is what I have . I have about 20 files in different packages in a STAND ALONE JAVA APPLICATION.
I am trying to use and write log files.
Following is my log4j.properties file which is in my class path:
log4j.appender.R = org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File = /ParentFolder/ChildFolder/application.log
log4j.appender.R.Append = true
log4j.appender.R.DatePattern = '.'yyy-MM-dd
log4j.appender.R.layout = org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} %c{1} [%p] %m%n
Following is the code to initialize logging in my main method
final String LOG_FILE = "C:/eclipse_workspace/lib/log4j.properties";
Properties logProp = new Properties();
try
{
logProp.load(new FileInputStream (LOG_FILE));
PropertyConfigurator.configure(logProperties);
logger.info("Logging enabled");
}
catch(IOException e)
{
System.out.println("Logging not enabled");
}
In every java class of the application I have the following code
import org.apache.log4j.*;
private static final Logger logger = Logger.getLogger(TheActualClassName.class);
But I get the following warning messages when I run the app.
log4j:WARN No appenders could be found for logger (com.xxx.myApp.MainProgram.MyFileName).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
What am I doing wrong?? The log file "application.log" is not being generated
May need the following line:
# Set root logger level to INFO and appender to R.
log4j.rootLogger=INFO, R
The root logger is always available and does not have a name.
Since the version 1.2.7, log4j (with the LogManager class) looks for log4j.xml in the classpath first. If the log4j.xml not exists, then log4j (with the LogManager class) looks for log4j.properties in the classpath.
Default Initialization Procedure
LogManager xref
If you are going to use a file named log4j.properties, and it's on your application's classpath, there is no need to even call PropertyConfiguration or DOMConfigurator - log4j will do this automatically when it is first initialized (when you first load a logger).
The error message seems to indicate that your configuration is not being loaded.
Add the VM argument -Dlog4j.debug to your application to have log4j spit out a whole bunch of information when it starts up, which includes which files it tries to load and what values it finds in the configuration.
Raghu ,if you are using stand alone configuration for configuring log4j Properties then use can use BasicConfigurator.configure() method for solving your appenders issue.

log4j:WARN Please initialize the log4j system properly

How to resolve these following errors... Am I missing some jar file???
log4j:WARN No appenders could be found for logger (smslib).
log4j:WARN Please initialize the log4j system properly.
org.smslib.GatewayException: Comm library exception: java.lang.RuntimeException: javax.comm.PortInUseException: Port currently owned by Unknown Windows Application
at org.smslib.modem.SerialModemDriver.connectPort(SerialModemDriver.java:97)
at org.smslib.modem.AModemDriver.connect(AModemDriver.java:110)
at org.smslib.modem.ModemGateway.startGateway(ModemGateway.java:158)
at org.smslib.Service$1Starter.run(Service.java:252)
WaitCommEvent: Error 31
WaitCommEvent: Error 31
My first thought was that your log4j XML or properties file wasn't picked up when log4j initialized. Make sure one of them is in your CLASSPATH.
A more careful reading of your exception suggests that you're trying to use a port that a Windows app already has taken control of.
Do a "netstat -a" to see what ports are currently in use and what's attached to them. Pick another one for your log4j appender to use.
Have you written a custom appender to write log messages to SMS? Something else is using the port you've chosen.
EDIT:
The jre/lib directory is not in the CLASSPATH. You should not be putting any of your code in that directory. It should end up in the directory where your compiled .class files are written to.

Categories