{"id":256263,"date":"2022-11-16T09:55:13","date_gmt":"2022-11-16T01:55:13","guid":{"rendered":"https:\/\/gulass.cn\/?p=256263"},"modified":"2022-10-28T12:56:33","modified_gmt":"2022-10-28T04:56:33","slug":"go-log-linux","status":"publish","type":"post","link":"https:\/\/gulass.cn\/go-log-linux.html","title":{"rendered":"\u7b80\u5355\u4ecb\u7ecdGo\u8bed\u8a00\u5e38\u7528\u7684\u6253log\u65b9\u5f0f"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nGolang\u7684log\u5305\u77ed\u5c0f\u7cbe\u608d,\u53ef\u4ee5\u975e\u5e38\u8f7b\u677e\u7684\u5b9e\u73b0\u65e5\u5fd7\u6253\u5370\u8f6c\u5b58\u529f\u80fd,\u4e0b\u9762\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u4e86\u5173\u4e8eGo\u8bed\u8a00\u5e38\u7528\u7684\u6253log\u65b9\u5f0f\u7684\u76f8\u5173\u8d44\u6599,\u6587\u4e2d\u901a\u8fc7\u5b9e\u4f8b\u4ee3\u7801\u4ecb\u7ecd\u7684\u975e\u5e38\u8be6\u7ec6,\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e0b<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u6587\u7ae0\u4ecb\u7ecd\u4e863\u79cd\u5e38\u7528\u7684\u6253log\u7684\u65b9\u5f0f\uff0c\u5e76\u8d4b\u4e0a\u4e86\u6ce8\u89e3<\/strong><\/div>\n

\u5148\u5199\u4e0a\u6ce8\u610f\u4e8b\u9879\uff0c\u4e09\u79cd\u65b9\u6cd5\u5efa\u8bae\u5206\u5f00\u6d4b\uff0c\u56e0\u4e3a\u53ef\u80fd\u5728\u7b2c\u4e00\u79cd\u7684Fatal\u4f1a\u7ec8\u6b62\u8fd0\u884c,Fatal\u548cPanic\u548cPrint\u7684\u533a\u522b\u5728\u4e8e,Fatal\u4f1a\u4fdd\u5b58\u65e5\u5fd7\u5e76\u7ec8\u6b62\u7a0b\u5e8f,Panic\u4f1a\u4fdd\u5b58\u65e5\u5fd7\u5e76\u4e22\u51fa\u5f02\u5e38\u7ec8\u6b62\u7a0b\u5e8f\uff0cPrint\u4f1a\u4fdd\u5b58\u65e5\u5fd7\u4f46\u662f\u7a0b\u5e8f\u7ee7\u7eed<\/p>\n

main.go<\/p>\n

package main\r\n \r\nimport (\r\n    \"log\"\r\n    \"bytes\"\r\n    \"fmt\"\r\n    \"os\"\r\n\"github.com\/astaxie\/beego\/logs\"\r\n)\r\n \r\nfunc main() {\r\n \r\n    \/\/1.\u65e5\u5fd7\u5199\u5165\u4e00\u6bb5\u7f13\u5b58\u91cc\r\n    var buf bytes.Buffer\r\n    logger := log.New(&buf, \"logger: \", log.Llongfile)\/\/\u4e09\u4e2a\u53c2\u6570\u5bf9\u5e94\u8f93\u51fa\u5bf9\u8c61\uff0c\u524d\u7f00\uff0c\u6587\u4ef6\u540d\u7136\u540e\u6574\u5408\u6210\u524d\u7f00\r\n    logger.Print(\"Hello, log file!\") \/\/\u5b9e\u9645log\u4fe1\u606f\r\n    fmt.Println(&buf)\r\n \r\n    \/\/2.\u65e5\u5fd7\u5199\u5165\u4e00\u6bb5\u6587\u4ef6\u91cc,\u6267\u884c\u5b8c\u5237\u65b0\u5de5\u7a0b\uff0c\u4f1a\u6709\u6587\u4ef6debug.log\u6253\u5f00\u6765\u770b\r\n    fileName := \"debug.log\"  \/\/\u5728\u5de5\u7a0b\u8def\u5f84\u4e0b\u548csrc\u540c\u7ea7\uff0c\u4e5f\u53ef\u4ee5\u5199\u7edd\u5bf9\u8def\u5f84\uff0c\u4e0d\u8fc7\u8981\u6ce8\u610f\u8f6c\u4e49\u7b26\r\n    logFile,err  := os.Create(fileName)  \/\/\u521b\u5efa\u8be5\u6587\u4ef6\uff0c\u8fd4\u56de\u53e5\u67c4\r\n    defer logFile.Close()      \/\/\u786e\u4fdd\u6587\u4ef6\u5728\u8be5\u51fd\u6570\u6267\u884c\u5b8c\u4ee5\u540e\u5173\u95ed\r\n    if err != nil {  \r\n        log.Fatalln(\"open file error !\")  \r\n    }  \r\n \r\n    \/\/\u5e93\u91cc\u7684log\u5305\u4e0d\u5b58\u5728\u7ea7\u522b\uff0c\u53ea\u6709Print\uff0cFatal\uff0cPanic\u4e09\u79cd\uff0c\u60f3\u89817\u79cd\u7ea7\u522b\uff0c\u53ef\u4ee5\u7528syslog\u5305\u6216\u8005\u6846\u67b6\r\n    debugLog := log.New(logFile,\"\",log.Llongfile)  \r\n    debugLog.Print(\"\u627e\u5230\u4e00\u4e2a\u4f4e\u7ea7bug,\u4f4e\u7ea7\u522b\u7684\u65e5\u5fd7\u62a5\u544a\uff0c\u6267\u884cdebug\u8bb0\u5f55\u540e\u7ee7\u7eed\u6267\u884c\")\r\n    debugLog.Fatal(\"\u53d1\u73b0\u4e00\u4e2a\u4e25\u91cdbug\uff0c\u65e5\u5fd7\u5df2\u8bb0\u5f55\uff0c\u7a0b\u5e8f\u5728\u6b64\u7ec8\u6b62\")\r\n    debugLog.Panic(\"\u54ce\u5440\uff0c\u6211\u6ca1\u6709\u88ab\u6267\u884c\uff0c\u597d\u60e8\")\r\n \r\n    \/\/3.\u4f7f\u7528beego\u6846\u67b6\u7684log\u5305\u8fdb\u884clog\u5904\u7406 \uff0c\u5bf9\u5e947\u79cd\u7ea7\u522b\r\n    logInstance:=logs.GetLogger()\r\n    logInstance.Println(\"this is a message of http\")\r\n    \/\/an official log.Logger with prefix ORM\r\n    logs.GetLogger(\"ORM\").Println(\"this is a message of orm\")\r\n    logs.Debug(\"my book is bought in the year of \", 2016)\r\n    logs.Info(\"this %s cat is %v years old\", \"yellow\", 3)\r\n    logs.Warn(\"json is a type of kv like\", map[string]int{\"key\": 2016})\r\n    logs.Error(1024, \"is a very\", \"good game\")\r\n    logs.Critical(\"oh,crash\")\r\n    logs.Notice(\"fuck\")\r\n    logs.Alert(\"alert\")\r\n    logs.Emergency(\"emergency\")\r\n \r\n}<\/pre>\n
\u7b2c\u4e8c\u79cd<\/strong><\/div>\n

main.go<\/p>\n

package main\r\n \r\nimport (\r\n    \"os\"\r\n    \"log\"\r\n)\r\n \r\n \r\n \r\nfunc main() {\r\n    file, err := os.OpenFile(\".\/testLog\/test.log\",  os.O_CREATE | os.O_WRONLY | os.O_APPEND,os.ModePerm)\r\n    if err != nil {\r\n        log.Fatalln(err)\r\n    }\r\n    logger := log.New(file, \"\", log.LstdFlags|log.Llongfile)\r\n    logger.Println(\"\u65e5\u5fd71.\")\r\n    logger.Println(\"\u65e5\u5fd723\")\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"

\u5148\u5199\u4e0a\u6ce8\u610f\u4e8b\u9879\uff0c\u4e09\u79cd\u65b9\u6cd5\u5efa\u8bae\u5206\u5f00\u6d4b\uff0c\u56e0\u4e3a\u53ef\u80fd\u5728\u7b2c\u4e00\u79cd\u7684Fatal\u4f1a\u7ec8\u6b62\u8fd0\u884c,Fatal\u548cPanic\u548cPrint […]<\/p>\n","protected":false},"author":1482,"featured_media":244616,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-256263","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/256263","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/1482"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=256263"}],"version-history":[{"count":3,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/256263\/revisions"}],"predecessor-version":[{"id":256266,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/256263\/revisions\/256266"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/244616"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=256263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=256263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=256263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}