Chinaunix首页 | 论坛 | 博客
  • 博客访问: 334156
  • 博文数量: 214
  • 博客积分: 4258
  • 博客等级: 上校
  • 技术积分: 2021
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-02 09:16
个人简介

http://blog.csdn.net/ly21st http://ly21st.blog.chinaunix.net

文章分类

全部博文(214)

文章存档

2018年(16)

2015年(1)

2014年(2)

2012年(22)

2011年(173)

分类: C/C++

2018-01-27 23:26:28

log各种格式日志输出

点击(此处)折叠或打开

  1. package main

  2. import (
  3.     "bytes"
  4.     "fmt"
  5.     "log"
  6. )

  7. func ExampleLogger() {

  8.     var (
  9.         buf bytes.Buffer
  10.         logger = log.New(&buf, "logger: ", log.Lshortfile)
  11.     )

  12.     logger.Print("Hello, log file!\n")
  13.     fmt.Print(&buf)

  14.     var buf1 bytes.Buffer
  15.     var logger1 = log.New(&buf1, "logger1: ", log.Ldate)
  16.     logger1.Print("Hello, log file!\n")
  17.     fmt.Print(&buf1)

  18.     var buf2 bytes.Buffer
  19.     var logger2 = log.New(&buf2, "logger2: ", log.Ltime)
  20.     logger2.Print("Hello, log file!\n")
  21.     fmt.Print(&buf2)

  22.     var buf3 bytes.Buffer
  23.     var logger3 = log.New(&buf3, "logger3: ", log.Lmicroseconds)
  24.     logger3.Print("Hello, log file!\n")
  25.     fmt.Print(&buf3)

  26.     var buf4 bytes.Buffer
  27.     var logger4 = log.New(&buf4, "logger4: ", log.Llongfile)
  28.     logger4.Print("Hello, log file!\n")
  29.     fmt.Print(&buf4)

  30.     var buf5 bytes.Buffer
  31.     var logger5 = log.New(&buf5, "logger5: ", log.Lshortfile)
  32.     logger5.Print("Hello, log file!\n")
  33.     fmt.Print(&buf5)

  34.     var buf6 bytes.Buffer
  35.     var logger6 = log.New(&buf6, "logger6: ", log.LUTC)
  36.     logger6.Print("Hello, log file!\n")
  37.     fmt.Print(&buf6)

  38.     var buf7 bytes.Buffer
  39.     var logger7 = log.New(&buf7, "logger7: ", log.LstdFlags)
  40.     logger7.Print("Hello, log file!\n")
  41.     fmt.Print(&buf7)

  42.     // Output:
  43.     // logger: example_test.go:19: Hello, log
  44. }

  45. func main() {
  46.     ExampleLogger()
  47. }
结果输出:
logger: main.go:16: Hello, log file!
logger1: 2018/01/27 Hello, log file!
logger2: 23:15:55 Hello, log file!
logger3: 23:15:55.273132 Hello, log file!
logger4: C:/Users/liyuan/go/src/file_io/main.go:36: Hello, log file!
logger5: main.go:41: Hello, log file!
logger6: Hello, log file!
logger7: 2018/01/27 23:15:55 Hello, log file!
成功: 进程退出代码 0.

------------------------------------------------------------------------

点击(此处)折叠或打开

  1. package main

  2. import (
  3.     "bytes"
  4.     "fmt"
  5.     "log"
  6. )

  7. func ExampleLogger_Output() {
  8.     var (
  9.         buf bytes.Buffer
  10.         logger = log.New(&buf, "INFO: ", log.Lshortfile)

  11.         infof = func(info string) {
  12.             logger.Output(2, info)
  13.         }
  14.     )

  15.     infof("Hello world")

  16.     fmt.Print(&buf)
  17.     // Output:
  18.     // INFO: example_test.go:36: Hello world
  19. }

  20. func main() {
  21.     ExampleLogger_Output()
  22. }
结果:
INFO: main.go:19: Hello world
成功: 进程退出代码 0.


阅读(411) | 评论(0) | 转发(0) |
0

上一篇:io操作

下一篇:服务器客户端例子

给主人留下些什么吧!~~