zoukankan      html  css  js  c++  java
  • 【go】log

    log

    package main
    
    import (
    	"log"
    	"os"
    )
    
    //init 在 main 之前执行
    func init() {
    	// 设置前缀
    	log.SetPrefix("Log-Test ") //Log-Test2021/05/30 23:39:29 cant create file
    
    	//设置输出
    	// 创建日志文件名,还有日志文件的属性
    	f, err := os.OpenFile("./go-log.log", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
    	if err != nil {
    		log.Fatalln("cant create file")
    	}
    	// 接收任何实现了 io.writer 接口的类型都可以
    	log.SetOutput(f)
    
    	// 设置标签
    	log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds | log.Llongfile) //日期 时间(默认就有)  + 微秒+文件明和行号
    	//Log-Test 2021/05/30 23:54:55.941390 /Users/yanyi/Desktop/Project/Go/study_go/go_project/src/FromBirthToDeath/pkg/log_t.go:50: println
    	// 有一堆的标签,默认是 LstdFlags     = Ldate | Ltime   3
    
    	// const (
    	//	Ldate         = 1 << iota     // the date in the local time zone: 2009/01/23
    	//	Ltime                         // the time in the local time zone: 01:23:23
    	//	Lmicroseconds                 // microsecond resolution: 01:23:23.123123.  assumes Ltime.
    	//	Llongfile                     // full file name and line number: /a/b/c/d.go:23
    	//	Lshortfile                    // final file name element and line number: d.go:23. overrides Llongfile
    	//	LUTC                          // if Ldate or Ltime is set, use UTC rather than the local time zone
    	//	Lmsgprefix                    // move the "prefix" from the beginning of the line to before the message
    	//	LstdFlags     = Ldate | Ltime // initial values for the standard logger
    	//)
    
    	// iota常量中定义依次为 0,1,2,3,4
    	//移位的作用,
    	//Ldate         = 1 << iota     1<<0  1向左移了 0 位还是 1, 00000001=1 2的0次
    	//Ltime              			1<<1  1向左移了 1 位是 2, 00000010=2 2的1次
    	//Lmicroseconds              	1<<2  1向左移了 2 位是 4, 00000100=4 2的2次
    
    	//这样的话可以实现组合, 00000011 代表的就是 Ldate 和 Ltime的组合
    
    }
    
    func main() {
    	log.Println("println") //ln带换行
    	//2021/05/30 23:33:17 println
    
    	//log.Fatalln("Fatalln")
    	//2021/05/30 23:33:37 Fatalln
    	//同时程序退出  相当于把信息记录日志后 同时执行了 OS.Exit(1 )
    
    	//log.Panicln("Panicln")
    	//log.Panic("Panicln") //不带换行的
    	//log.Panicf("Panicln %d",555) //带格式化的
    	//panic: Panicln
    	//
    	//
    	//goroutine 1 [running]:
    	//log.Panicln(0xc000070f68, 0x1, 0x1)
    	//        /usr/local/go/src/log/log.go:368 +0xae
    	//main.main()
    	//        /Users/yanyi/Desktop/Project/Go/study_go/go_project/src/FromBirthToDeath/pkg/log_t.go:17 +0x5d
    
    	//程序退出同时打出 panic 的原因
    }
    
    
  • 相关阅读:
    DesignPattern系列__10单例模式
    DesignPattern系列__09设计模式概述
    DesignPattern系列__08UML相关知识
    DesignPattern系列__07合成复用原则
    DesignPattern系列__06迪米特原则
    爬取猫眼电影top100电影
    安卓微信对接H5微信支付出现“商家参数有误,请联系商家解决”的问题处理
    python 通过使用pandas的实现的Excel的批量转换CSV文件的处理
    输入一个字符串,判断字符串中最大对称字串的长度
    面向对象六大设计原则(转载)
  • 原文地址:https://www.cnblogs.com/cizao/p/14829674.html
Copyright © 2011-2022 走看看