# package log
`import "log"`
log包实现了简单的日志服务。本包定义了Logger类型,该类型提供了一些格式化输出的方法。本包也提供了一个预定义的“标准”Logger,可以通过辅助函数Print[f|ln]、Fatal[f|ln]和Panic[f|ln]访问,比手工创建一个Logger对象更容易使用。Logger会打印每条日志信息的日期、时间,默认输出到标准错误。Fatal系列函数会在写入日志信息后调用os.Exit(1)。Panic系列函数会在写入日志信息后panic。
## Index
* [Constants](#pkg-constants)
* [type Logger](#Logger)
* [func New(out io.Writer, prefix string, flag int) \*Logger](#New)
* [func (l \*Logger) Flags() int](#Logger.Flags)
* [func (l \*Logger) SetFlags(flag int)](#Logger.SetFlags)
* [func (l \*Logger) Prefix() string](#Logger.Prefix)
* [func (l \*Logger) SetPrefix(prefix string)](#Logger.SetPrefix)
* [func (l \*Logger) Output(calldepth int, s string) error](#Logger.Output)
* [func (l \*Logger) Printf(format string, v ...interface{})](#Logger.Printf)
* [func (l \*Logger) Print(v ...interface{})](#Logger.Print)
* [func (l \*Logger) Println(v ...interface{})](#Logger.Println)
* [func (l \*Logger) Fatalf(format string, v ...interface{})](#Logger.Fatalf)
* [func (l \*Logger) Fatal(v ...interface{})](#Logger.Fatal)
* [func (l \*Logger) Fatalln(v ...interface{})](#Logger.Fatalln)
* [func (l \*Logger) Panic(v ...interface{})](#Logger.Panic)
* [func (l \*Logger) Panicf(format string, v ...interface{})](#Logger.Panicf)
* [func (l \*Logger) Panicln(v ...interface{})](#Logger.Panicln)
* [func Flags() int](#Flags)
* [func SetFlags(flag int)](#SetFlags)
* [func Prefix() string](#Prefix)
* [func SetPrefix(prefix string)](#SetPrefix)
* [func SetOutput(w io.Writer)](#SetOutput)
* [func Printf(format string, v ...interface{})](#Printf)
* [func Print(v ...interface{})](#Print)
* [func Println(v ...interface{})](#Println)
* [func Fatalf(format string, v ...interface{})](#Fatalf)
* [func Fatal(v ...interface{})](#Fatal)
* [func Fatalln(v ...interface{})](#Fatalln)
* [func Panicf(format string, v ...interface{})](#Panicf)
* [func Panic(v ...interface{})](#Panic)
* [func Panicln(v ...interface{})](#Panicln)
### Examples
* [Logger](#example-Logger)
## Constants
```
const (
// 字位共同控制输出日志信息的细节。不能控制输出的顺序和格式。
// 在所有项目后会有一个冒号:2009/01/23 01:23:23.123123 /a/b/c/d.go:23: message
Ldate = 1 << iota // 日期:2009/01/23
Ltime // 时间:01:23:23
Lmicroseconds // 微秒分辨率:01:23:23.123123(用于增强Ltime位)
Llongfile // 文件全路径名+行号: /a/b/c/d.go:23
Lshortfile // 文件无路径名+行号:d.go:23(会覆盖掉Llongfile)
LstdFlags = Ldate | Ltime // 标准logger的初始值
)
```
这些选项定义Logger类型如何生成用于每条日志的前缀文本。
## type [Logger](https://github.com/golang/go/blob/master/src/log/log.go#L42 "View Source")
```
type Logger struct {
// contains filtered or unexported fields
}
```
Logger类型表示一个活动状态的记录日志的对象,它会生成一行行的输出写入一个io.Writer接口。每一条日志操作会调用一次io.Writer接口的Write方法。Logger类型的对象可以被多个线程安全的同时使用,它会保证对io.Writer接口的顺序访问。
Example
```
var buf bytes.Buffer
logger := log.New(&buf, "logger: ", log.Lshortfile)
logger.Print("Hello, log file!")
fmt.Print(&buf)
```
Output:
```
logger: example_test.go:16: Hello, log file!
```
### func [New](https://github.com/golang/go/blob/master/src/log/log.go#L54 "View Source")
```
func New(out io.Writer, prefix string, flag int) *Logger
```
New创建一个Logger。参数out设置日志信息写入的目的地。参数prefix会添加到生成的每一条日志前面。参数flag定义日志的属性(时间、文件等等)。
### func (\*Logger) [Flags](https://github.com/golang/go/blob/master/src/log/log.go#L211 "View Source")
```
func (l *Logger) Flags() int
```
Flags返回logger的输出选项。
### func (\*Logger) [SetFlags](https://github.com/golang/go/blob/master/src/log/log.go#L218 "View Source")
```
func (l *Logger) SetFlags(flag int)
```
SetFlags设置logger的输出选项。
### func (\*Logger) [Prefix](https://github.com/golang/go/blob/master/src/log/log.go#L225 "View Source")
```
func (l *Logger) Prefix() string
```
Prefix返回logger的输出前缀。
### func (\*Logger) [SetPrefix](https://github.com/golang/go/blob/master/src/log/log.go#L232 "View Source")
```
func (l *Logger) SetPrefix(prefix string)
```
SSetPrefix设置logger的输出前缀。
### func (\*Logger) [Output](https://github.com/golang/go/blob/master/src/log/log.go#L130 "View Source")
```
func (l *Logger) Output(calldepth int, s string) error
```
Output写入输出一次日志事件。参数s包含在Logger根据选项生成的前缀之后要打印的文本。如果s末尾没有换行会添加换行符。calldepth用于恢复PC,出于一般性而提供,但目前在所有预定义的路径上它的值都为2。
### func (\*Logger) [Printf](https://github.com/golang/go/blob/master/src/log/log.go#L159 "View Source")
```
func (l *Logger) Printf(format string, v ...interface{})
```
Printf调用l.Output将生成的格式化字符串输出到logger,参数用和fmt.Printf相同的方法处理。
### func (\*Logger) [Print](https://github.com/golang/go/blob/master/src/log/log.go#L165 "View Source")
```
func (l *Logger) Print(v ...interface{})
```
Print调用l.Output将生成的格式化字符串输出到logger,参数用和fmt.Print相同的方法处理。
### func (\*Logger) [Println](https://github.com/golang/go/blob/master/src/log/log.go#L169 "View Source")
```
func (l *Logger) Println(v ...interface{})
```
Println调用l.Output将生成的格式化字符串输出到logger,参数用和fmt.Println相同的方法处理。
### func (\*Logger) [Fatalf](https://github.com/golang/go/blob/master/src/log/log.go#L178 "View Source")
```
func (l *Logger) Fatalf(format string, v ...interface{})
```
Fatalf等价于{l.Printf(v...); os.Exit(1)}
### func (\*Logger) [Fatal](https://github.com/golang/go/blob/master/src/log/log.go#L172 "View Source")
```
func (l *Logger) Fatal(v ...interface{})
```
Fatal等价于{l.Print(v...); os.Exit(1)}
### func (\*Logger) [Fatalln](https://github.com/golang/go/blob/master/src/log/log.go#L184 "View Source")
```
func (l *Logger) Fatalln(v ...interface{})
```
Fatalln等价于{l.Println(v...); os.Exit(1)}
### func (\*Logger) [Panicf](https://github.com/golang/go/blob/master/src/log/log.go#L197 "View Source")
```
func (l *Logger) Panicf(format string, v ...interface{})
```
Panicf等价于{l.Printf(v...); panic(...)}
### func (\*Logger) [Panic](https://github.com/golang/go/blob/master/src/log/log.go#L190 "View Source")
```
func (l *Logger) Panic(v ...interface{})
```
Panic等价于{l.Print(v...); panic(...)}
### func (\*Logger) [Panicln](https://github.com/golang/go/blob/master/src/log/log.go#L204 "View Source")
```
func (l *Logger) Panicln(v ...interface{})
```
Panicln等价于{l.Println(v...); panic(...)}
## func [Flags](https://github.com/golang/go/blob/master/src/log/log.go#L246 "View Source")
```
func Flags() int
```
Flags返回标准logger的输出选项。
## func [SetFlags](https://github.com/golang/go/blob/master/src/log/log.go#L251 "View Source")
```
func SetFlags(flag int)
```
SetFlags设置标准logger的输出选项。
## func [Prefix](https://github.com/golang/go/blob/master/src/log/log.go#L256 "View Source")
```
func Prefix() string
```
Prefix返回标准logger的输出前缀。
## func [SetPrefix](https://github.com/golang/go/blob/master/src/log/log.go#L261 "View Source")
```
func SetPrefix(prefix string)
```
SetPrefix设置标准logger的输出前缀。
## func [SetOutput](https://github.com/golang/go/blob/master/src/log/log.go#L239 "View Source")
```
func SetOutput(w io.Writer)
```
SetOutput设置标准logger的输出目的地,默认是标准错误输出。
## func [Printf](https://github.com/golang/go/blob/master/src/log/log.go#L275 "View Source")
```
func Printf(format string, v ...interface{})
```
Printf调用Output将生成的格式化字符串输出到标准logger,参数用和fmt.Printf相同的方法处理。
## func [Print](https://github.com/golang/go/blob/master/src/log/log.go#L269 "View Source")
```
func Print(v ...interface{})
```
Print调用Output将生成的格式化字符串输出到标准logger,参数用和fmt.Print相同的方法处理。
## func [Println](https://github.com/golang/go/blob/master/src/log/log.go#L281 "View Source")
```
func Println(v ...interface{})
```
Println调用Output将生成的格式化字符串输出到标准logger,参数用和fmt.Println相同的方法处理。
## func [Fatalf](https://github.com/golang/go/blob/master/src/log/log.go#L292 "View Source")
```
func Fatalf(format string, v ...interface{})
```
Fatalf等价于{Printf(v...); os.Exit(1)}
## func [Fatal](https://github.com/golang/go/blob/master/src/log/log.go#L286 "View Source")
```
func Fatal(v ...interface{})
```
Fatal等价于{Print(v...); os.Exit(1)}
## func [Fatalln](https://github.com/golang/go/blob/master/src/log/log.go#L298 "View Source")
```
func Fatalln(v ...interface{})
```
Fatalln等价于{Println(v...); os.Exit(1)}
## func [Panicf](https://github.com/golang/go/blob/master/src/log/log.go#L311 "View Source")
```
func Panicf(format string, v ...interface{})
```
Panicf等价于{Printf(v...); panic(...)}
## func [Panic](https://github.com/golang/go/blob/master/src/log/log.go#L304 "View Source")
```
func Panic(v ...interface{})
```
Panic等价于{Print(v...); panic(...)}
## func [Panicln](https://github.com/golang/go/blob/master/src/log/log.go#L318 "View Source")
```
func Panicln(v ...interface{})
```
Panicln等价于{Println(v...); panic(...)}
- 库
- package achive
- package tar
- package zip
- package bufio
- package builtin
- package bytes
- package compress
- package bzip2
- package flate
- package gzip
- package lzw
- package zlib
- package container
- package heap
- package list
- package ring
- package crypto
- package aes
- package cipher
- package des
- package dsa
- package ecdsa
- package elliptic
- package hmac
- package md5
- package rand
- package rc4
- package rsa
- package sha1
- package sha256
- package sha512
- package subtle
- package tls
- package x509
- package pkix
- package database
- package sql
- package driver
- package encoding
- package ascii85
- package asn1
- package base32
- package base64
- package binary
- package csv
- package gob
- package hex
- package json
- package pem
- package xml
- package errors
- package expvar
- package flag
- package fmt
- package go
- package doc
- package format
- package parser
- package printer
- package hash
- package adler32
- package crc32
- package crc64
- package fnv
- package html
- package template
- package image
- package color
- package palette
- package draw
- package gif
- package jpeg
- package png
- package index
- package suffixarray
- package io
- package ioutil
- package log
- package syslog
- package math
- package big
- package cmplx
- package rand
- package mime
- package multipart
- package net
- package http
- package cgi
- package cookiejar
- package fcgi
- package httptest
- package httputil
- package pprof
- package mail
- package rpc
- package jsonrpc
- package smtp
- package textproto
- package url
- package os
- package exec
- package signal
- package user
- package path
- package filepath
- package reflect
- package regexp
- package runtime
- package cgo
- package debug
- package pprof
- package race
- package sort
- package strconv
- package strings
- package sync
- package atomic
- package text
- package scanner
- package tabwriter
- package template
- package time
- package unicode
- package utf16
- package utf8
- package unsafe