Go output log file: Advanced golAND output log file
Recently write TCP communication, many times need to do log analysis, so write a loghelp, simple and convenient. Ha ha ha ~, hope to help you.
About: Go/Goland: log
My own use, the feeling is very comfortable: the use of log, I am relatively shallow, but also hope that you can give more advice
Advanced version: you can create your own files, by small time files, by talent folder. It’s very comfortable. Here is the source code: copy after you can run directly to see the result
/* * Copyright(C),2019-2020, email: [email protected] * Author: DAO * Version: 1.0.0 * Date: 2021/6/11 13:54 * Description: * */
package main
import (
"fmt"
"io"
"log"
"os"
"time"
"strings"
)
var logFile io.Writer
/** * @description: Checks whether the given path is a file * @param path * @return os.FileInfo * @return bool */
func IsFile(path string) (os.FileInfo, bool) {
f, flag := IsExists(path)
returnf, flag && ! f.IsDir() }/** * @description: Checks whether the path exists * @param path * @return os.FileInfo * @return bool */
func IsExists(path string) (os.FileInfo, bool) {
f, err := os.Stat(path)
return f, err == nil || os.IsExist(err)
}
/** * @description: write log * @param MSG * @param title */
func Logwrite(msg string, title string) {
// Set the time variable
now := time.Now()
// Prerequisite: Create a log folder in the root directory (this is best). If the file directory does not exist, you can also use the following code to create a file directory
folderName := "log/" + now.Format("20060102")
folderTemp := ""
for _, v := range strings.Split(folderName, "/") {
// Determine the file, if it does not exist, then create it
folderTemp += v
_, IsExistsPath := IsExists(folderTemp)
if! IsExistsPath {// Create folder code
os.Mkdir(folderTemp, os.ModePerm)
}
folderTemp += "/"
}
// Set up the log file and manage the log file: here is a time file, every hour to generate a different file
filefullname := ". /" + folderName + "/" + now.Format("20060102") + now.Format("_15") + ".txt"
// Check whether the file exists
_, b := IsFile(filefullname)
if b {
// Open the file,
logFile, _ = os.OpenFile(filefullname, os.O_APPEND, 0666)}else {
// Create a new file
logFile, _ = os.Create(filefullname)
}
loger := log.New(logFile, "log_", log.Ldate|log.Ltime|log.Lshortfile)
//SetFlags sets output options
loger.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
// Set the output prefix, if the prefix is not transmitted, then no output
if title == "" {
loger.SetPrefix("log_")}else {
loger.SetPrefix(title + "_")}// Set the header information
rs := []rune(msg)
msghead := ""
// For aesthetics: wrap lines if there are more than 20 characters
if len(rs) >= 20 {
msghead = string(rs[0:20])
// Output a log: header information, 10 bits, can be adjusted by the above parameter: rs[0:10]
loger.Output(2, msghead+"[omitted...]. \n"+msg+"\n")}else {
loger.Output(2, msg+"\n")}// Prints on the console
fmt.Println(msg)
}
func main(a) {
// Output 20 words of information, using main as the header
Logwrite("wewewee"."Main")
// Output more than 20 characters of information, using the default header
Logwrite("wewewee11111111111111111111111111111111111112222222222222222222222222"."")}Copy the code
Effects: Console:Log file: