{"id":5174,"date":"2018-04-09T18:14:05","date_gmt":"2018-04-09T09:14:05","guid":{"rendered":"http:\/\/www.gisdeveloper.co.kr\/?p=5174"},"modified":"2020-05-28T13:55:29","modified_gmt":"2020-05-28T04:55:29","slug":"java%ec%9d%98-%ea%b8%b0%eb%b3%b8-log-%ea%b8%b0%eb%8a%a5-%ec%a0%95%eb%a6%ac","status":"publish","type":"post","link":"http:\/\/www.gisdeveloper.co.kr\/?p=5174","title":{"rendered":"Java\uc758 \uae30\ubcf8 log \uae30\ub2a5 \uc815\ub9ac"},"content":{"rendered":"<p>log4j \ub4f1\uacfc \uac19\uc740 \ub85c\uadf8\ub97c \uc704\ud55c \uc88b\uc740 \ub77c\uc774\ube0c\ub7ec\ub9ac\uac00 \uc788\uc73c\ub098 Java\uc5d0\uc11c \uc81c\uacf5\ud558\ub294 \uae30\ubcf8 Log \uae30\ub2a5\uc5d0 \ub300\ud574 \uc815\ub9ac\ud569\ub2c8\ub2e4. \uba3c\uc800 \uac00\uc7a5 \uac04\ub2e8\ud55c \uc608\ub294 \uc544\ub798\uc640 \uac19\uc2b5\ub2c8\ub2e4. log\uc5d0 \ub300\ud55c \uc218\uc900(Level)\uc744 \uc9c0\uc815\ud574 \uba54\uc138\uc9c0\ub97c \ucf58\uc194\uc5d0 \ud45c\uc2dc\ud558\ub294 \uacbd\uc6b0\uc785\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">\r\npackage tst_Log_console;\r\n\r\nimport java.util.logging.Level;\r\nimport java.util.logging.Logger;\r\n\r\npublic class MainEntry {\r\n    private final static Logger LOG = Logger.getGlobal();\r\n\t\r\n    public static void main(String[] args) {\r\n        LOG.setLevel(Level.INFO);\r\n\t\t\r\n        LOG.severe(\"severe Log\");\r\n        LOG.warning(\"warning Log\");\r\n        LOG.info(\"info Log\");\r\n    }\r\n}\r\n<\/pre>\n<p>Log\uc758 \uc218\uc900\uc740 \ub192\uc740 \uc21c\uc11c\ub85c SEVERE, WARNING, INFO\uc785\ub2c8\ub2e4. \uac01\uac01\uc774 \uc758\ubbf8\ud558\ub294 \ubc14\ub294 \uc2ec\uac01\ud568, \uacbd\uace0, \uc815\ubcf4\uc785\ub2c8\ub2e4. setLevel \ud568\uc218\ub97c \ud1b5\ud574 \ud45c\uc2dc\ud560 \ub808\ubca8\uc758 \uc218\uc900\uc744 \uc870\uc815\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4. \uc704 \ucf54\ub4dc\uc5d0 \ub300\ud55c \uc2e4\ud589 \uacb0\uacfc\ub294 \ub2e4\uc74c\uacfc \uac19\uc2b5\ub2c8\ub2e4.<\/p>\n<p><code>4\uc6d4 09, 2018 5:58:41 \uc624\ud6c4 tst_Log_console.MainEntry main<br \/>\n\uc2ec\uac01: severe Log<br \/>\n4\uc6d4 09, 2018 5:58:41 \uc624\ud6c4 tst_Log_console.MainEntry main<br \/>\n\uacbd\uace0: warning Log<br \/>\n4\uc6d4 09, 2018 5:58:41 \uc624\ud6c4 tst_Log_console.MainEntry main<br \/>\n\uc815\ubcf4: info Log<\/code><\/p>\n<p>\uc704\ucc98\ub7fc \ub85c\uadf8\uc758 \ub0b4\uc6a9\uc740 \uc774\ubbf8 \uc815\ud574\uc838 \uc788\ub294\ub370\uc694. \ub9cc\uc57d \uc790\uc2e0\ub9cc\uc758 \ub85c\uadf8 \ud615\uc2dd\uc744 \uc6d0\ud55c\ub2e4\uba74 \uba3c\uc800 Formatter \ud074\ub798\uc2a4\ub97c \uc0c1\uc18d\ubc1b\uc544 \ub2e4\uc74c\uacfc \uac19\uc740 \ud074\ub798\uc2a4\ub97c \uc815\uc758\ud574\uc57c \ud569\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">\r\npackage tst_Log_console;\r\n\r\nimport java.text.SimpleDateFormat;\r\nimport java.util.Date;\r\nimport java.util.logging.Formatter;\r\nimport java.util.logging.Handler;\r\nimport java.util.logging.LogRecord;\r\n\r\npublic class CustomLogFormatter extends Formatter {\r\n    public String getHead(Handler h) {\r\n        return \"START LOG\\n\";\r\n    }\r\n\t\r\n    public String format(LogRecord rec) {\r\n        StringBuffer buf = new StringBuffer(1000);\r\n\r\n        buf.append(calcDate(rec.getMillis()));\r\n        \r\n        buf.append(\" [\");\r\n        buf.append(rec.getLevel());\r\n        buf.append(\"] \");\r\n        \r\n        buf.append(\"[\");\r\n        buf.append(rec.getSourceMethodName());\r\n        buf.append(\"] \");\r\n        \r\n        buf.append(rec.getMessage());\r\n        buf.append(\"\\n\");\r\n        \r\n        return buf.toString();\r\n    }\r\n\r\n    public String getTail(Handler h) {\r\n    \treturn \"END LOG\\n\";\r\n    }\r\n    \r\n    private String calcDate(long millisecs) {\r\n        SimpleDateFormat date_format = new SimpleDateFormat(\"yyyy-MM-dd HH:mm\");\r\n        Date resultdate = new Date(millisecs);\r\n        return date_format.format(resultdate);\r\n    }\r\n}\r\n<\/pre>\n<p>\uadf8\ub9ac\uace0 \uc774 \ud074\ub798\uc2a4\ub97c \ub2e4\uc74c\uc758 \uc608\ucc98\ub7fc \ud65c\uc6a9\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">\r\npackage tst_Log_console;\r\n\r\nimport java.util.logging.ConsoleHandler;\r\nimport java.util.logging.Handler;\r\nimport java.util.logging.Level;\r\nimport java.util.logging.Logger;\r\n\r\npublic class MainEntry2 {\r\n    private final static Logger LOG = Logger.getGlobal();\r\n\t\r\n    public static void main(String[] args) {\r\n        \/\/=============================================\r\n        \/\/ \uae30\ubcf8 \ub85c\uadf8 \uc81c\uac70\r\n        \/\/------------\r\n        Logger rootLogger = Logger.getLogger(\"\");\r\n        Handler[] handlers = rootLogger.getHandlers();\r\n        if (handlers[0] instanceof ConsoleHandler) {\r\n            rootLogger.removeHandler(handlers[0]);\r\n        }\r\n        \/\/=============================================\r\n\t\t\r\n        LOG.setLevel(Level.INFO);\r\n\t\t\r\n        Handler handler = new ConsoleHandler();\r\n        CustomLogFormatter formatter = new CustomLogFormatter();\r\n        handler.setFormatter(formatter);\r\n        LOG.addHandler(handler);\r\n\t\t\r\n        LOG.severe(\"severe Log\");\r\n        LOG.warning(\"warning Log\");\r\n        LOG.info(\"info Log\");\r\n    }\r\n}\r\n<\/pre>\n<p>\uc704\uc758 \ud074\ub798\uc2a4\uc5d0\uc11c getHead \ud568\uc218\ub294 \ub85c\uadf8 \uae30\ub85d\uc744 \uc2dc\uc791\ud560\ub54c \ud55c\ubc88\ub9cc \ud638\ucd9c\ub418\uc5b4 \ub85c\uadf8\ub85c\uc368 \ud45c\uc2dc\ub418\ub294 \ubb38\uc790\uc5f4\uc744 \ubc18\ud658\ud569\ub2c8\ub2e4. \uadf8\ub9ac\uace0 format\uc740 \uac1c\ubc1c\uc790\uac00 \ub85c\uadf8\ub97c \ud45c\uc2dc\ud558\uae30 \uc6d0\ud560\ub54c\ub9c8\ub2e4 \uba54\uc138\uc9c0\uc758 \ud615\uc2dd\uc744 \uc815\uc758\ud558\uae30 \uc704\ud574 \ud638\ucd9c\ub418\ub294 \ud568\uc218\uc785\ub2c8\ub2e4. \uadf8\ub9ac\uace0 getTail\uc740 \ud504\ub85c\uadf8\ub7a8\uc774 \uc885\ub8cc\ub420\ub54c \ud638\ucd9c\ub418\ub294 \ub85c\uadf8 \uba54\uc138\uc9c0\ub85c \ud45c\uc2dc\ud560 \ubb38\uc790\uc5f4\uc744 \ubc18\ud658\ud569\ub2c8\ub2e4. (\uadf8\ub7ec\ub098 \ud604\uc7ac \uc2dc\uc810\uc5d0\uc11c \uc774 getTail\uc740 \ub85c\uadf8\ub97c \ucf58\uc194\ub85c \ud45c\uc2dc\ud560 \ub54c \ud638\ucd9c\ub418\uc9c0 \uc54a\ub294 \ubb38\uc81c\uac00 \uc788\uc74c) \ucf54\ub4dc\ub97c \uc880\ub354 \uc0b4\ud3b4\ubcf4\uba74, 12-20\ubc88 \ucf54\ub4dc\ub294 \uae30\ubcf8\uc801\uc73c\ub85c \uc9c0\uc815\ub41c \ub85c\uadf8(\ucf58\uc194\ucc3d\uc5d0 \uc774\ubbf8 \uc815\uc758\ub41c \ud615\uc2dd\uc73c\ub85c \ub0b4\uc694\uc744 \ucd9c\ub825\ud568)\ub97c \uc81c\uac70\ud569\ub2c8\ub2e4. \uadf8\ub9ac\uace0 24-27\ubc88 \ucf54\ub4dc\ucc98\ub7fc \uc55e\uc11c \uc815\uc758\ud55c Formatter\uc5d0 \ub300\ud55c \uc0c1\uc18d \ud074\ub798\uc2a4\ub97c \uc0dd\uc131\ud574 \uc9c0\uc815\ud569\ub2c8\ub2e4. \uc2e4\ud589\ud574 \ubcf4\uba74 \ub2e4\uc74c\uacfc \uac19\uc2b5\ub2c8\ub2e4.<\/p>\n<p><code>START LOG<br \/>\n2018-04-09 18:06 [SEVERE] [main] severe Log<br \/>\n2018-04-09 18:06 [WARNING] [main] warning Log<br \/>\n2018-04-09 18:06 [INFO] [main] info Log<\/code><\/p>\n<p>\uc704\uc758 \uacbd\uc6b0\ucc98\ub7fc \ub85c\uadf8\ub97c \ud654\uba74\uc774 \uc544\ub2cc \ud30c\uc77c\ub85c \uc800\uc7a5\ud558\uace0 \uc2f6\uc744 \ub54c\uac00 \uc788\uc2b5\ub2c8\ub2e4. \uc774 \uacbd\uc6b0 \uc544\ub798\uc758 \ucf54\ub4dc\uc640 \uac19\uc774 \ud558\uba74 \ub429\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">\r\npackage tst_Log_console;\r\n\r\nimport java.io.IOException;\r\nimport java.util.logging.ConsoleHandler;\r\nimport java.util.logging.FileHandler;\r\nimport java.util.logging.Handler;\r\nimport java.util.logging.Level;\r\nimport java.util.logging.Logger;\r\n\r\npublic class MainEntry3 {\r\n    private final static Logger LOG = Logger.getGlobal();\r\n\t\r\n    public static void main(String[] args) throws SecurityException, IOException {\r\n        \/\/=============================================\r\n        \/\/ \uae30\ubcf8 \ub85c\uadf8 \uc81c\uac70\r\n        \/\/------------\r\n        Logger rootLogger = Logger.getLogger(\"\");\r\n        Handler[] handlers = rootLogger.getHandlers();\r\n        if (handlers[0] instanceof ConsoleHandler) {\r\n            rootLogger.removeHandler(handlers[0]);\r\n        }\r\n        \/\/=============================================\r\n\t\t\r\n        LOG.setLevel(Level.INFO);\r\n\t\t\r\n        Handler handler = new FileHandler(\"message.log\", true);\r\n        \r\n        CustomLogFormatter formatter = new CustomLogFormatter();\r\n        handler.setFormatter(formatter);\r\n        LOG.addHandler(handler);\r\n\t\t\r\n        LOG.severe(\"severe Log\");\r\n        LOG.warning(\"warning Log\");\r\n        LOG.info(\"info Log\");\r\n    }\r\n}\r\n<\/pre>\n<p>\ub2ec\ub77c\uc9c0\ub294 \ub0b4\uc6a9\uc740 26\ubc88 \ucf54\ub4dc\uc5d0\uc11c ConsoleHandler \ub300\uc2e0 FileHandler\ub85c \ubcc0\uacbd\ub418\uc5c8\ub2e4\ub294 \uac83\uc785\ub2c8\ub2e4. \uc774 FileHandler \uc0dd\uc131\uc790\ub294 \uccab\ubc88\uc9f8 \uc778\uc790\uc5d0 \uc0ac\uc6a9\ud560 \ud30c\uc77c\uba85\uc744, \uadf8\ub9ac\uace0 \ub450\ubc88\uc9f8 \uc778\uc790\uc5d0 \ud30c\uc77c \uae30\ub85d\uc2dc APPEND \ubaa8\ub4dc\uc778\uc9c0\uc758 \uc5ec\ubd80\ub97c \uc9c0\uc815\ud569\ub2c8\ub2e4.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>log4j \ub4f1\uacfc \uac19\uc740 \ub85c\uadf8\ub97c \uc704\ud55c \uc88b\uc740 \ub77c\uc774\ube0c\ub7ec\ub9ac\uac00 \uc788\uc73c\ub098 Java\uc5d0\uc11c \uc81c\uacf5\ud558\ub294 \uae30\ubcf8 Log \uae30\ub2a5\uc5d0 \ub300\ud574 \uc815\ub9ac\ud569\ub2c8\ub2e4. \uba3c\uc800 \uac00\uc7a5 \uac04\ub2e8\ud55c \uc608\ub294 \uc544\ub798\uc640 \uac19\uc2b5\ub2c8\ub2e4. log\uc5d0 \ub300\ud55c \uc218\uc900(Level)\uc744 \uc9c0\uc815\ud574 \uba54\uc138\uc9c0\ub97c \ucf58\uc194\uc5d0 \ud45c\uc2dc\ud558\ub294 \uacbd\uc6b0\uc785\ub2c8\ub2e4. package tst_Log_console; import java.util.logging.Level; import java.util.logging.Logger; public class MainEntry { private final static Logger LOG = Logger.getGlobal(); public static void main(String[] args) { LOG.setLevel(Level.INFO); LOG.severe(&#8220;severe Log&#8221;); &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/www.gisdeveloper.co.kr\/?p=5174\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Java\uc758 \uae30\ubcf8 log \uae30\ub2a5 \uc815\ub9ac&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[60],"tags":[],"class_list":["post-5174","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/5174","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5174"}],"version-history":[{"count":7,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/5174\/revisions"}],"predecessor-version":[{"id":9611,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/5174\/revisions\/9611"}],"wp:attachment":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5174"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5174"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}