funcmain() { // load config file, it's optional // or log.LoadConfiguration("./example.json", "json") // config file could be json or xml log.LoadConfiguration("./example.json")
// 这里的 Test 是配置文件里面的 category 定义的 log.LOGGER("Test").Info("category Test info test ...") log.LOGGER("Test").Info("category Test info test ... %d", 123) log.LOGGER("Test").Info("category Test info test message: %s", "new test msg") log.LOGGER("Test").Debug("category Test debug test ...")
// Other category not exist, test log.LOGGER("Other").Debug("category Other debug test ...")
// socket log test log.LOGGER("TestSocket").Debug("category TestSocket debug test ...")
// original log4go test log.Info("nomal info test ...") log.Debug("nomal debug test ...")
log.Close() }
输出样例
[2017/11/15 14:35:11 CST] [Test] [INFO] (main.main:15) category Test info test ... [2017/11/15 14:35:11 CST] [Test] [INFO] (main.main:16) category Test info test message: new test msg [2017/11/15 14:35:11 CST] [Test] [DEBG] (main.main:17) category Test debug test ... [2017/11/15 14:35:11 CST] [DEFAULT] [INFO] (main.main:26) nomal info test ... [2017/11/15 14:35:11 CST] [DEFAULT] [DEBG] (main.main:27) nomal debug test ...
if val, err := PathExists(path + "/logs/"); err != nil { logger.Error("log path not exist exception") } else { if val == false { //0755->即用户具有读/写/执行权限,组用户和其它用户具有读写权限; //0644->即用户具有读写权限,组用户和其它用户具有只读权限; err := os.MkdirAll(path+"/logs/", 0755) if err != nil { fmt.Printf("%s", err) } else { fmt.Print("Create Directory OK!") } } }
logFileName = path + "/logs/" + time.Now().Format("2006-01-02") + ".log" // Create the logger file if doesn't exist. And append to it if it already exists. // per-log should be less than 4k f, err := os.OpenFile(logFileName, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
//Logging to file Formatter := new(logrus.TextFormatter) Formatter.TimestampFormat = "2006-01-02 15:04:05" Formatter.FullTimestamp = true logrus.SetFormatter(Formatter) if err != nil { // Cannot open logger file. Logging to stderr fmt.Println(err) } else { logger.Out = f } logger.Info("logger setup ok. gisHelper app started.")