Display FINE log level in output - java

how can i display the fine log message in output screen in java?

You might want to configure other defaults with a properties file. This allow things to be reconfigured without recompiling.
# Specify the handlers to create in the root logger
# (all loggers are children of the root logger)
# The following creates two handlers
handlers = java.util.logging.ConsoleHandler, java.util.logging.FileHandler
# Set the default logging level for the root logger
.level = ALL
# Set the default logging level for new ConsoleHandler instances
java.util.logging.ConsoleHandler.level = INFO
# Set the default logging level for new FileHandler instances
java.util.logging.FileHandler.level = ALL
# Set the default formatter for new ConsoleHandler instances
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
# Set the default logging level for the logger named com.mycompany
com.mycompany.level = ALL
I prefer this to littering my code with logging configuration calls.
You have to specify the location of the file with a command line option though :
java -Djava.util.logging.config.file=mylogging.properties
I personally always use log4j or slf4j because it looks for a config file in the classpath. Well, maybe java.util.logging does that too, I never really investigated.

Related

Adding a variable into all log messages

I'm using org.slf4j:slf4j-api:1.7.26 to write logs from a Java application. My log4j.properties looks like:
log4j.rootLogger=WARN, console
# add a ConsoleAppender to the logger stdout to write to the console
log4j.appender.console=org.apache.log4j.ConsoleAppender
# use a simple message format
log4j.appender.console.layout=org.apache.log4j.EnhancedPatternLayout
log4j.appender.console.layout.ConversionPattern={\
"time":"%d{ISO8601}",\
"level":"%p",\
"class":"%c{1.}",\
"message":"%m",\
"thread":"%t",\
"fileName":"PLACEHOLDER_FILE_NAME"\
}%n
I need to replace PLACEHOLDER_FILE_NAME with the name of a file that is fed into the application as a CLA at startup. rather How can this be done? I've looked into java.util.logging.Formatter but I'm not convinced that's the best way as it seems to override log4j.properties.
You can read system property eg. ${file.name} in log4j config:
Then, you just need to set this property before log4j reads this config eg. at startup specyfying it as -Dfile.name=filenamePath
log4j.rootLogger=WARN, console
# add a ConsoleAppender to the logger stdout to write to the console
log4j.appender.console=org.apache.log4j.ConsoleAppender
# use a simple message format
log4j.appender.console.layout=org.apache.log4j.EnhancedPatternLayout
log4j.appender.console.layout.ConversionPattern={\
"time":"%d{ISO8601}",\
"level":"%p",\
"class":"%c{1.}",\
"message":"%m",\
"thread":"%t",\
"fileName":"${file.name}"\
}%n

How can I straighten out my Tomcat logging?

Am working on a Tomcat configuration I did not do and would like any help to fix the logging. For reasons unknown, there are three -D parameters related to logging passed into the startup. The process looks like:
./bin/java -Djava.util.logging.config.file=CATALINA_BASE_DIR_HERE/tomcat/conf/logging.properties
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
-Dlog4j.configuration=file:CATALINA_BASE_DIR_HERE/tomcat/conf/log4j.properties ...
The relevant section of the logging.properties file looks as shown below. I believe this is standard.
handlers = 1catalina.org.apache.juli.FileHandler, 2localhost.org.apache.juli.FileHandler, 3manager.org.apache.juli.FileHandler, 4host-manager.org.apache.juli.FileHandler, java.util.logging.ConsoleHandler
...
2localhost.org.apache.juli.FileHandler.level = FINE
2localhost.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
2localhost.org.apache.juli.FileHandler.prefix = localhost.
For completeness, here is relevant portion of the log4j.properties file:
log4j.rootLogger=INFO, logfile
log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.logfile.DatePattern='.'yyyy-MM-dd
log4j.appender.logfile.File=MY_DIR/my_log.txt
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern = [%d{ABSOLUTE}] [%t] %-5p [%c] - %m%n
log4j.appender.logfile.Append=true
# per first answer given below, added:
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost] = INFO, logfile
Prior to today, there were no real issues - all the log data went to the log4j file as desired. The war file deployed in this Tomcat uses Spring Data JPA and Hibernate. To debug, showSql was enabled. All the queries came out, but to the localhost.DATE.log file. Can anyone tell how to fix so that ALL the statements go to the log4j designated file?
If you set showSql to true, hibernate will print SQL statement to SystemOut. You should add log4j.logger.org.hibernate.SQL=DEBUG to log4j config, so hibernate can also log the SQL statement. (The reason can be found in this answer)
By default, Tomcat use java.util.logging API for all internal logging. So the output goto localhost.DATE.log as you mentioned.
You can change the configuration, please refer the section Using Log4j
(for Tomcat 6.x~8.x)

How to change log level of userlogs in Hadoop 2?

I've been trying to change the log level on userlogs i.e the files that appear under /var/log/hadoop-yarn/userlogs/application_<id>/container_<id> on CDH 5.2.1. However, no matter what I try, only INFO level logs will appear. I want to enable TRACE level logs for debugging.
Things I have tried so far:
Setting all loggers to TRACE level in /etc/hadoop/conf/log4j.properties.
Setting mapreduce.map.log.level and mapreduce.reduce.log.level in mapred-site.xml.
Setting mapreduce.map.log.level and mapreduce.reduce.log.level in the job configuration before submitting it.
Including a log4j.properties in my job jar file that sets the root Log4j logger to TRACE.
Modifying yarn-env.sh to specify YARN_ROOT_LOGGER=TRACE,console
None of these worked -- they didn't break anything, but they didn't have any effect on the log outputs under the userlogs directory. Modifying yarn-env.sh did cause the ResourceManager and NodeManager logs to enter trace level. Unfortunately these are not useful for my purpose.
I get the following error appearing in /var/log/hadoop-yarn/userlogs/application_<id>/container_<id>/stderr that may be relevant.
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/usr/lib/zookeeper/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/tmp/hadoop-yarn/nm-local-dir/usercache/tomcat/appcache/application_1419961570089_0001/filecache/10/job.jar/lib/slf4j-log4j12-1.7.5.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.Log4jLoggerFactory]
log4j:WARN No appenders could be found for logger (org.apache.hadoop.ipc.Server).
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 don't understand why the log4j "no configuration" message would happen, given that there is a log4j.properties file at the root of the job jar file that specifies a root logger:
log4j.rootLogger=TRACE, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] %m%n
My code does not knowingly use SLF4J for logging, it purely uses Log4j.
The actual answer was to set yarn.app.mapreduce.am.log.level to the level you need, but, crucially, it needs to be set in the Hadoop job configuration at submission time. It cannot be set on the cluster globally. The cluster global will always default to INFO, as it is hardcoded.
Using container-log4j.properties alone will not work as YARN will override the log level value on the command line. See the method addLog4jSystemProperties of org.apache.hadoop.mapreduce.v2.util.MRApps and cross reference with org.apache.hadoop.mapreduce.MRJobConfig.
container-log4j.properties will indeed be honoured, but it can't override the level set by this property.
On CDH5.8, I am overwriting the application log level at job submission time. To modify the log level of the mappers and reducers, I had to specify them explicitly using a command like this:
hadoop jar my-mapreduce-job.jar -libjars ${LIBJARS} -Dyarn.app.mapreduce.am.log.level=DEBUG,console -Dmapreduce.map.log.level=DEBUG,console -Dmapreduce.reduce.log.level=DEBUG,console
The logs are written to the applications's syslog which can be browsed using the Yarn ResourceManager Web UI.
You should try to edit (or create if it does'nt exist) : /etc/hadoop/conf/container-log4j.properties.
You can see it with a simple ps aux : the command line includes -Dlog4j.configuration=container-log4j.properties.

Logger logs are not showing up in Console and Tomcat logs

Well I have my Tomcat logging configured and I know the properties are being read, because it now genereates the log files according to the name prefix that I specified. Well the problems is that the log files contain all the info that Tomcat itself logs, however it doesn't contain anything from what I try to output in my servlet classes with log.info(...) etc
my logging.properties:
handlers = org.apache.juli.FileHandler, java.util.logging.ConsoleHandler
############################################################
# Handler specific properties.
# Describes specific configuration info for Handlers.
############################################################
org.apache.juli.FileHandler.level = FINEST
org.apache.juli.FileHandler.directory = ${catalina.base}/logs
org.apache.juli.FileHandler.prefix = GDIA_
java.util.logging.ConsoleHandler.level = FINEST
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
As you can see it's all set as visible from FINEST, however I don't see any output when i do:
My imports:
import java.util.logging.Level;
import java.util.logging.Logger;
My log variable:
private final static Logger log = Logger.getLogger(GenTreeUploader.class.getName());
My testing the output:
log.severe("GREAT!!!!!!!!!!");
I'm running Tomcat 7 in Eclipse. What's interesting is that before I added configuration to tomcat run configuration in eclipse The log output would work fine to console, but whenever I added this:
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.util.logging.config.file=D:\Dropbox\EclipseWorkspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp7\conf\logging.properties"
I couldn't see any output in console or in the log file. Of course prior to adding this my logging.properties were not to be found...
So any ideas why when I add this configuration I can't see any log output from the application classes when I do "log.severe("GREAT!!!!!!!!!!");"...?

Logging to different files using single logging.properties

I'm trying to configure the logging.properties file that is shared between multiple applications. I want a specific log file for a class of mine, but I don't want other logs (from other classes) to be included in the file.
How can I achieve this by only modifying the logging.properties file?
I have the following logging.properties file:
############################################################
# Default Logging Configuration File
#
# You can use a different file by specifying a filename
# with the java.util.logging.config.file system property.
# For example java -Djava.util.logging.config.file=myfile
############################################################
############################################################
# Global properties
############################################################
# "handlers" specifies a comma separated list of log Handler
# classes. These handlers will be installed during VM startup.
# Note that these classes must be on the system classpath.
# By default we only configure a ConsoleHandler, which will only
# show messages at the INFO and above levels.
handlers= java.util.logging.ConsoleHandler
# To also add the FileHandler, use the following line instead.
#handlers= java.util.logging.FileHandler, java.util.logging.ConsoleHandler
# Default global logging level.
# This specifies which kinds of events are logged across
# all loggers. For any given facility this global level
# can be overriden by a facility specific level
# Note that the ConsoleHandler also has a separate level
# setting to limit messages printed to the console.
.level= INFO
############################################################
# Handler specific properties.
# Describes specific configuration info for Handlers.
############################################################
# default file output is in user's home directory.
java.util.logging.FileHandler.pattern = %h/default.log (all the logs are currently going to this file)
java.util.logging.FileHandler.limit = 50000
java.util.logging.FileHandler.count = 1
java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter
# Limit the message that are printed on the console to INFO and above.
java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
############################################################
# Facility specific properties.
# Provides extra control for each logger.
############################################################
# For example, set the com.xyz.foo logger to only log SEVERE
# messages:
my.class.logger.level = INFO (I want this class to log to the file /home/myuser/myclass.log)
other.class.logger.level = ALL
It is possible using pure jdk also (try with jdk 7 or jdk 8).
Just create custom file handler; use that similar to java.util.logging.FileHandler and extend it.
public class JULTestingFileHandler extends FileHandler {
public JULTestingFileHandler() throws IOException, SecurityException
{
super();
}
}
user properties file;
com.xxx.handlers = com.xxx.JULXXXFileHandler
com.xxx.JULXXXFileHandler.pattern = ./logs/test1_test2.%u.%g.log

Categories

Resources