{"id":7121,"date":"2013-01-15T13:25:10","date_gmt":"2013-01-15T18:25:10","guid":{"rendered":"http:\/\/jianmingli.com\/wp\/?p=7121"},"modified":"2013-01-16T18:04:48","modified_gmt":"2013-01-16T23:04:48","slug":"jdk-logging","status":"publish","type":"post","link":"https:\/\/jianmingli.com\/wp\/?p=7121","title":{"rendered":"JDK Logging"},"content":{"rendered":"<div class='toc wptoc'>\n<h2>Contents<\/h2>\n<ol class='toc-odd level-1'>\n\t<li>\n\t\t<a href=\"#Overview\">Overview<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#Sample_Config_File:_mylogging.properties\">Sample Config File: mylogging.properties<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#Point_to_Custom_Logging_Properties_File\">Point to Custom Logging Properties File<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#Use_Logging_in_Java_Code\">Use Logging in Java Code<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#References\">References<\/a>\n\t<\/li>\n<\/ol>\n<\/ol>\n<\/ol>\n<\/div>\n<div class='wptoc-end'>&nbsp;<\/div>\n<span id=\"Overview\"><h3>Overview<\/h3><\/span>\n<p>* Default config file is located at: <em>JDK_HOME\/jre\/lib\/logging.properties<\/em><\/p>\n<span id=\"Sample_Config_File:_mylogging.properties\"><h3>Sample Config File: mylogging.properties<\/h3><\/span>\n<p>* Output to both console and file named <em>java.log<\/em> in user home directory.<br \/>\n* Log only INFO level and above (i.e. INFO, WARNING and SERVERE)<br \/>\n* Append to existing log entries.<\/p>\n<pre lang=\"text\">\r\n############################################################\r\n#  \tDefault Logging Configuration File\r\n#\r\n# You can use a different file by specifying a filename\r\n# with the java.util.logging.config.file system property.  \r\n# For example java -Djava.util.logging.config.file=myfile\r\n############################################################\r\n\r\n############################################################\r\n#  \tGlobal properties\r\n############################################################\r\n\r\n# \"handlers\" specifies a comma separated list of log Handler \r\n# classes.  These handlers will be installed during VM startup.\r\n# Note that these classes must be on the system classpath.\r\n# By default we only configure a ConsoleHandler, which will only\r\n# show messages at the INFO and above levels.\r\nhandlers= java.util.logging.ConsoleHandler, java.util.logging.FileHandler\r\n\r\n# To also add the FileHandler, use the following line instead.\r\n#handlers= java.util.logging.FileHandler, java.util.logging.ConsoleHandler\r\n\r\n# Default global logging level.\r\n# This specifies which kinds of events are logged across\r\n# all loggers.  For any given facility this global level\r\n# can be overriden by a facility specific level\r\n# Note that the ConsoleHandler also has a separate level\r\n# setting to limit messages printed to the console.\r\n.level= INFO\r\n\r\n############################################################\r\n# Handler specific properties.\r\n# Describes specific configuration info for Handlers.\r\n############################################################\r\n\r\n# default file output is in user's home directory.\r\njava.util.logging.FileHandler.pattern = %h\/java%u.log\r\njava.util.logging.FileHandler.limit = 50000\r\njava.util.logging.FileHandler.count = 1\r\njava.util.logging.FileHandler.append = true\r\njava.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter\r\n\r\n# Limit the message that are printed on the console to INFO and above.\r\njava.util.logging.ConsoleHandler.level = INFO\r\njava.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter\r\n\r\n# Example to customize the SimpleFormatter output format \r\n# to print one-line log message like this:\r\n#     <level>: <log message> [<date\/time>]\r\n#\r\n# java.util.logging.SimpleFormatter.format=%4$s: %5$s [%1$tc]%n\r\n\r\n############################################################\r\n# Facility specific properties.\r\n# Provides extra control for each logger.\r\n############################################################\r\n\r\n# For example, set the com.xyz.foo logger to only log SEVERE\r\n# messages:\r\ncom.xyz.foo.level = SEVERE\r\n<\/pre>\n<span id=\"Point_to_Custom_Logging_Properties_File\"><h3>Point to Custom Logging Properties File<\/h3><\/span>\n<p>* Replace <em>JDK_HOME\/jre\/lib\/logging.properties<\/em> file<br \/>\n* From command line:<\/p>\n<pre lang=\"bash\">\r\njava -Djava.util.logging.config.file=C:\/users\/john.doe\/mylogging.properties\r\n<\/pre>\n<p>* From Java code:<\/p>\n<pre lang=\"java\">\r\nString mylogfile = System.getProperty(\"user.home\") + \"\/mylogging.properties\";\r\nLogManager.getLogManager().readConfiguration(\r\n   new FileInputStream(mylogfile ));\r\nSystem.setProperty(\"java.util.logging.config.file\", mylogfile);\r\n<\/pre>\n<span id=\"Use_Logging_in_Java_Code\"><h3>Use Logging in Java Code<\/h3><\/span>\n<pre lang=\"java\">\r\nstatic final Logger logger =\r\n  Logger.getLogger(MyTestClass.class.getName());\r\n...\r\n\r\n@Test\r\npublic void testLogging() throws Exception {\r\n\tKrbUtils.setLoggingProperties();\r\n\tlogger.severe(\"Test severe message.\");\r\n\tlogger.warning(\"Test warning message.\");\r\n\tlogger.info(\"Test info message.\");\r\n\tlogger.config(\"Test config message.\");\r\n\tlogger.fine(\"Test fine message.\");\r\n\tlogger.finer(\"Test finer message.\");\r\n\tlogger.finest(\"Test finest message.\");\r\n}\r\n<\/pre>\n<p>* Output:<\/p>\n<pre lang=\"bash\">\r\nJan 15, 2013 1:24:03 PM com.ngc.security.utils.KrbUtilsTest testLogging\r\nSEVERE: Test severe message.\r\nJan 15, 2013 1:24:03 PM com.ngc.security.utils.KrbUtilsTest testLogging\r\nWARNING: Test warning message.\r\nJan 15, 2013 1:24:03 PM com.ngc.security.utils.KrbUtilsTest testLogging\r\nINFO: Test info message.\r\n<\/pre>\n<span id=\"References\"><h3>References<\/h3><\/span>\n<p>* <a href=\"http:\/\/docs.oracle.com\/javase\/1.4.2\/docs\/api\/index.html\">java.util.logging<\/a><br \/>\n* <a href=\"http:\/\/docs.oracle.com\/javase\/1.4.2\/docs\/api\/index.html\">java.util.logging.LogManager<\/a><br \/>\n* <a href=\"http:\/\/docs.oracle.com\/javase\/1.4.2\/docs\/api\/index.html\">java.util.logging.level<\/a><br \/>\n* <a href=\"http:\/\/docs.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/logging\/FileHandler.html\">http:\/\/docs.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/logging\/FileHandler.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Overview * Default config file is located at: JDK_HOME\/jre\/lib\/logging.properties Sample Config File: mylogging.properties * Output to both console and file named java.log in user home directory. * Log only INFO level and above (i.e. INFO, WARNING and SERVERE) * Append &hellip; <a href=\"https:\/\/jianmingli.com\/wp\/?p=7121\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[318],"tags":[317,631],"class_list":["post-7121","post","type-post","status-publish","format-standard","hentry","category-logging","tag-jdklogging","tag-logging"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8cRUO-1QR","_links":{"self":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/7121","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7121"}],"version-history":[{"count":5,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/7121\/revisions"}],"predecessor-version":[{"id":7127,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/7121\/revisions\/7127"}],"wp:attachment":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}