Tinylog capture System Error output stream to log file - java

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.

Related

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.

Log4j Configuration issues - log4j:WARN Please initialize the log4j system properly

I got error:
log4j:WARN No appenders could be found for logger (java.lang.Class).
log4j:WARN Please initialize the log4j system properly.
I have been through many threads and forums to fix the above error, but could not find the solution to my problem.
Problem: My requirement specifies us to use the below filenames for each environment.
log.dev
log.local
log.test
How to configure my application to detect these log files?
log4j must be properly configured for logging to files.
try this :
Logger logger = Logger.getLogger(yourclassname.class);
BasicConfigurator.configure(); // basic log4j configuration
Logger.getRootLogger().setLevel(Level.INFO);
FileAppender fileAppender = null;
try {
fileAppender =
new RollingFileAppender(new PatternLayout("%d{dd-MM-yyyy HH:mm:ss} %C %L %-5p:%m%n"),"file.log");
logger.addAppender(fileAppender);
} catch (IOException e) {
e.printStackTrace();
}
logger.info("TEST LOG ENTRY");
This should create a log file named file.log in the local folder.
Use your java program and logic to removeAppender and addAppender as necessary to switch files.
Or you can create multiple logger instances each with one fileAppender if switching is required dynamically throughout the program.
This way of using log4j avoids the need for external configuration file log4j.properties.

Easily disable log4j for applet?

I am building Java Desktop and Java Applet from the same code. Log4j is in use so almost every class in desktop version (which is original) has line like
private static final Logger LOG = LoggerFactory.getLogger(EachClassName.class);
Because Applet is not supposed to perform any io operations I had to exclude this configuration code:
InputStream is = Log4jConfigurator.class.getClassLoader().getResourceAsStream(path);
properties.load(is);
is.close();
PropertyConfigurator.configure(properties);
But now I get warnings because getLogger is still in place
log4j:WARN No appenders could be found for logger (org.game.client.Client).
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 would be the most elegant solution to avoid this warnings and use logger only in desktop version?
In your "Applet App" try to configure log4j programmatically:
turn off logging
LogManager.getRootLogger().setLevel(Level.OFF);
Add NullAppender (if setting Level.OFF is not enough)
LogManager.getRootLogger().addAppender(new NullAppender())

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.

How to change colour of logging output log4j [duplicate]

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

Categories