[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"academy-blogs-en-1-1-all-go-logging-event-recording-all--*":3,"academy-blog-translations-s91mfq9mlgrbbns":119},{"data":4,"page":107,"perPage":107,"totalItems":107,"totalPages":107},[5],{"alt":6,"collectionId":7,"collectionName":8,"content":9,"cover_image":10,"cover_image_path":11,"created":12,"created_by":13,"expand":14,"id":113,"keywords":114,"locale":89,"published_at":115,"scheduled_at":13,"school_blog":111,"short_description":116,"slug":117,"status":109,"title":6,"updated":118,"updated_by":13,"views":112},"Ep.17 Go and Logging - Recording Every Event in the Program","sclblg987654321","school_blog_translations","\u003Cp class=\"p1\">\u003Cstrong>Go and Logging - Recording Every Event in the Program\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p2\">&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>What is Logging?\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p3\">Logging is the process of recording data or messages that indicate what is happening in the program, such as logging when a database connection is successful or logging warning messages when an error occurs. Logging helps us troubleshoot issues more easily when the program malfunctions.\u003C\u002Fp>\u003Cp class=\"p2\">&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>Basic Usage of Logging in Go\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p3\">Go has a log package that makes it easy to implement logging using main functions like log.Print, log.Println, and log.Printf.\u003C\u002Fp>\u003Cp class=\"p3\">Explanation of the Code:\u003C\u002Fp>\u003Cp class=\"p3\">\u003Ci>log.Println prints a message followed by a new line.\u003C\u002Fi>\u003C\u002Fp>\u003Cp class=\"p3\">\u003Ci>log.Print prints a message.\u003C\u002Fi>\u003C\u002Fp>\u003Cp class=\"p3\">\u003Ci>log.Printf is used to format messages, such as using %d for numbers.\u003C\u002Fi>\u003C\u002Fp>\u003Cp class=\"p3\">Example of Logging Usage:\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">package main\r\n\r\nimport (\r\n    \"log\"\r\n)\r\n\r\nfunc main() {\r\n    log.Println(\"Starting the application...\")\r\n    log.Print(\"This is a regular log message.\")\r\n    log.Printf(\"This is a formatted log message: %d\", 123)\r\n}\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>Logging Errors and Fatal Logs\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p3\">In addition to logging regular messages, we can also log messages when errors occur using log.Fatal and log.Panic.\u003C\u002Fp>\u003Cp class=\"p3\">\u003Ci>log.Fatal: Logs the error and stops the program.\u003C\u002Fi>\u003C\u002Fp>\u003Cp class=\"p3\">\u003Ci>log.Panic: Logs the error and shows the stack trace before stopping the program.\u003C\u002Fi>\u003C\u002Fp>\u003Cp class=\"p3\">Example of Usage:\u003C\u002Fp>\u003Cp class=\"p3\">In this example, log.Fatal will log a message and immediately stop the program when an error occurs.\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">package main\r\n\r\nimport (\r\n    \"log\"\r\n    \"os\"\r\n)\r\n\r\nfunc main() {\r\n    file, err := os.Open(\"nonexistent_file.txt\")\r\n    if err != nil {\r\n        log.Fatal(\"Error opening file:\", err)\r\n    }\r\n    defer file.Close()\r\n}\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>Logging to a File Instead of Displaying on the Screen\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p3\">Sometimes we may want to store logs in a file instead of printing them to the screen for later review of events.\u003C\u002Fp>\u003Cp class=\"p3\">Example of Logging to a File:\u003C\u002Fp>\u003Cp class=\"p3\">In this example:\u003C\u002Fp>\u003Cp class=\"p3\">\u003Ci>log.SetOutput(file) allows log messages to be recorded in the app.log file.\u003C\u002Fi>\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">package main\r\n\r\nimport (\r\n    \"log\"\r\n    \"os\"\r\n)\r\n\r\nfunc main() {\r\n    file, err := os.OpenFile(\"app.log\", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)\r\n    if err != nil {\r\n        log.Fatal(\"Could not open log file:\", err)\r\n    }\r\n    defer file.Close()\r\n\r\n    log.SetOutput(file) \u002F\u002F ตั้งให้ log เขียนลงไฟล์แทนหน้าจอ\r\n\r\n    log.Println(\"Starting application...\")\r\n    log.Println(\"Logging information to file.\")\r\n}\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp class=\"p3\">&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>Using Flags to Add Details to Logs\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p3\">We can add details to logs by using flags to indicate that the log should show the date and time of the recorded messages.\u003C\u002Fp>\u003Cp class=\"p3\">Commonly Used Flags:\u003C\u002Fp>\u003Cp class=\"p3\">\u003Ci>log.LstdFlags: Shows the date and time.\u003C\u002Fi>\u003C\u002Fp>\u003Cp class=\"p3\">\u003Ci>log.Lshortfile: Shows the location of the file where the event occurred.\u003C\u002Fi>\u003C\u002Fp>\u003Cp class=\"p3\">Example:\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">log.SetFlags(log.LstdFlags | log.Lshortfile)\r\nlog.Println(\"This is a log message with date, time, and file location.\")\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>In Summary\u003C\u002Fstrong>\u003C\u002Fp>\u003Cul class=\"ul1\">\u003Cli class=\"li3\">Use log.Print, log.Println, and log.Printf for regular messages.\u003C\u002Fli>\u003Cli class=\"li3\">Use log.Fatal and log.Panic for logging errors and stopping the program.\u003C\u002Fli>\u003Cli class=\"li3\">Logs can be stored in files for long-term data retention.\u003C\u002Fli>\u003Cli class=\"li3\">Use flags to add date, time, and file location information to logs.\u003C\u002Fli>\u003C\u002Ful>","4_11zon_b7elbcd2qi.webp","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclblg987654321\u002Fzp5gy7uclxdkyc2\u002F4_11zon_b7elbcd2qi.webp","2026-03-04 08:34:35.563Z","",{"keywords":15,"locale":83,"school_blog":93},[16,23,28,33,38,43,48,53,58,63,68,73,78],{"collectionId":17,"collectionName":18,"created":19,"created_by":13,"id":20,"name":21,"updated":22,"updated_by":13},"sclkey987654321","school_keywords","2026-03-04 08:26:59.195Z","gab60xd583s3qaw","Superdev School","2026-04-10 16:07:37.087Z",{"collectionId":17,"collectionName":18,"created":24,"created_by":13,"id":25,"name":26,"updated":27,"updated_by":13},"2026-03-04 08:31:49.362Z","2m9vv13etpn6zkx","programming language","2026-04-10 16:07:45.606Z",{"collectionId":17,"collectionName":18,"created":29,"created_by":13,"id":30,"name":31,"updated":32,"updated_by":13},"2026-03-04 08:32:26.073Z","vnvj1oaxje9m1q8","programming for beginners","2026-04-10 16:07:54.133Z",{"collectionId":17,"collectionName":18,"created":34,"created_by":13,"id":35,"name":36,"updated":37,"updated_by":13},"2026-03-04 08:31:54.955Z","264sfjffyhspetq","programmers","2026-04-10 16:07:47.221Z",{"collectionId":17,"collectionName":18,"created":39,"created_by":13,"id":40,"name":41,"updated":42,"updated_by":13},"2026-03-04 08:31:22.575Z","lfjse4xivbgg5wu","Practice programming","2026-04-10 16:07:39.541Z",{"collectionId":17,"collectionName":18,"created":44,"created_by":13,"id":45,"name":46,"updated":47,"updated_by":13},"2026-03-04 08:32:15.843Z","m0x7wo77i8iycf1","Programming Education","2026-04-10 16:07:51.675Z",{"collectionId":17,"collectionName":18,"created":49,"created_by":13,"id":50,"name":51,"updated":52,"updated_by":13},"2026-03-04 08:34:32.087Z","kbrswxvizdfzrdz","log.Panic","2026-04-10 16:08:16.777Z",{"collectionId":17,"collectionName":18,"created":54,"created_by":13,"id":55,"name":56,"updated":57,"updated_by":13},"2026-03-04 08:34:33.669Z","kkc3qkdxnu2bi2k","log.Fatal","2026-04-10 16:08:17.351Z",{"collectionId":17,"collectionName":18,"created":59,"created_by":13,"id":60,"name":61,"updated":62,"updated_by":13},"2026-03-04 08:34:33.961Z","4m7k8rg9qp5698q","log.Print","2026-04-10 16:08:17.441Z",{"collectionId":17,"collectionName":18,"created":64,"created_by":13,"id":65,"name":66,"updated":67,"updated_by":13},"2026-03-04 08:34:34.243Z","g4xefd2eobhkwhw","Log Management","2026-04-10 16:08:17.604Z",{"collectionId":17,"collectionName":18,"created":69,"created_by":13,"id":70,"name":71,"updated":72,"updated_by":13},"2026-03-04 08:34:34.728Z","g8nh82ty64uf2hy","Event Recording","2026-04-10 16:08:17.810Z",{"collectionId":17,"collectionName":18,"created":74,"created_by":13,"id":75,"name":76,"updated":77,"updated_by":13},"2026-03-04 08:33:55.302Z","0mue4zt83jcdtq9","Logging","2026-04-10 16:08:03.396Z",{"collectionId":17,"collectionName":18,"created":79,"created_by":13,"id":80,"name":81,"updated":82,"updated_by":13},"2026-03-04 08:20:14.253Z","ah6lvy4x8qe08l5","Golang","2026-04-10 16:07:26.172Z",{"code":84,"collectionId":85,"collectionName":86,"created":87,"flag":88,"id":89,"is_default":90,"label":91,"updated":92},"en","pbc_1989393366","locales","2026-01-22 11:00:02.726Z","twemoji:flag-united-states","qv9c1llfov2d88z",false,"English","2026-04-10 15:42:46.825Z",{"category":94,"collectionId":95,"collectionName":96,"expand":97,"id":111,"views":112},"wqxt7ag2gn7xcmk","pbc_2105096300","school_blogs",{"category":98},{"blogIds":99,"collectionId":100,"collectionName":101,"created":102,"created_by":13,"id":94,"image":103,"image_alt":13,"image_path":104,"label":105,"name":106,"priority":107,"publish_at":108,"scheduled_at":13,"status":109,"updated":110,"updated_by":13},[],"sclcatblg987654321","school_category_blogs","2026-03-04 08:33:53.210Z","59ty92ns80w_15oc1implw.png","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclcatblg987654321\u002Fwqxt7ag2gn7xcmk\u002F59ty92ns80w_15oc1implw.png",{"en":106,"th":106},"Golang The Series",1,"2026-03-16 04:39:38.440Z","published","2026-04-25 02:32:15.470Z","s91mfq9mlgrbbns",212,"zp5gy7uclxdkyc2",[20,25,30,35,40,45,50,55,60,65,70,75,80],"2025-01-27 04:36:54.615Z","Learn how to use Logging in Go to record data and messages that occur in programs, including error logging and log management.","go-logging-event-recording","2026-04-25 02:47:33.550Z",{"en":117}]