1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package log implements a simple logging package. It defines a type, Logger,
6 // with methods for formatting output. It also has a predefined 'standard'
7 // Logger accessible through helper functions Print[f|ln], Fatal[f|ln], and
8 // Panic[f|ln], which are easier to use than creating a Logger manually.
9 // That logger writes to standard error and prints the date and time
10 // of each logged message.
11 // The Fatal functions call os.Exit(1) after writing the log message.
12 // The Panic functions call panic after writing the log message.
24 // These flags define which text to prefix to each log entry generated by the Logger.
26 // Bits or'ed together to control what's printed. There is no control over the
27 // order they appear (the order listed here) or the format they present (as
28 // described in the comments). A colon appears after these items:
29 // 2009/01/23 01:23:23.123123 /a/b/c/d.go:23: message
30 Ldate
= 1 << iota // the date: 2009/01/23
31 Ltime
// the time: 01:23:23
32 Lmicroseconds
// microsecond resolution: 01:23:23.123123. assumes Ltime.
33 Llongfile
// full file name and line number: /a/b/c/d.go:23
34 Lshortfile
// final file name element and line number: d.go:23. overrides Llongfile
35 LstdFlags
= Ldate | Ltime
// initial values for the standard logger
38 // A Logger represents an active logging object that generates lines of
39 // output to an io.Writer. Each logging operation makes a single call to
40 // the Writer's Write method. A Logger can be used simultaneously from
41 // multiple goroutines; it guarantees to serialize access to the Writer.
43 mu sync
.Mutex
// ensures atomic writes; protects the following fields
44 prefix
string // prefix to write at beginning of each line
45 flag
int // properties
46 out io
.Writer
// destination for output
47 buf
[]byte // for accumulating text to write
50 // New creates a new Logger. The out variable sets the
51 // destination to which log data will be written.
52 // The prefix appears at the beginning of each generated log line.
53 // The flag argument defines the logging properties.
54 func New(out io
.Writer
, prefix
string, flag
int) *Logger
{
55 return &Logger
{out
: out
, prefix
: prefix
, flag
: flag
}
58 var std
= New(os
.Stderr
, "", LstdFlags
)
60 // Cheap integer to fixed-width decimal ASCII. Give a negative width to avoid zero-padding.
61 // Knows the buffer has capacity.
62 func itoa(buf
*[]byte, i
int, wid
int) {
64 if u
== 0 && wid
<= 1 {
65 *buf
= append(*buf
, '0')
69 // Assemble decimal in reverse order.
72 for ; u
> 0 || wid
> 0; u
/= 10 {
75 b
[bp
] = byte(u%10
) + '0'
77 *buf
= append(*buf
, b
[bp
:]...)
80 func (l
*Logger
) formatHeader(buf
*[]byte, t time
.Time
, file
string, line
int) {
81 *buf
= append(*buf
, l
.prefix
...)
82 if l
.flag
&(Ldate|Ltime|Lmicroseconds
) != 0 {
83 if l
.flag
&Ldate
!= 0 {
84 year
, month
, day
:= t
.Date()
86 *buf
= append(*buf
, '/')
87 itoa(buf
, int(month
), 2)
88 *buf
= append(*buf
, '/')
90 *buf
= append(*buf
, ' ')
92 if l
.flag
&(Ltime|Lmicroseconds
) != 0 {
93 hour
, min
, sec
:= t
.Clock()
95 *buf
= append(*buf
, ':')
97 *buf
= append(*buf
, ':')
99 if l
.flag
&Lmicroseconds
!= 0 {
100 *buf
= append(*buf
, '.')
101 itoa(buf
, t
.Nanosecond()/1e3
, 6)
103 *buf
= append(*buf
, ' ')
106 if l
.flag
&(Lshortfile|Llongfile
) != 0 {
107 if l
.flag
&Lshortfile
!= 0 {
109 for i
:= len(file
) - 1; i
> 0; i
-- {
117 *buf
= append(*buf
, file
...)
118 *buf
= append(*buf
, ':')
120 *buf
= append(*buf
, ": "...)
124 // Output writes the output for a logging event. The string s contains
125 // the text to print after the prefix specified by the flags of the
126 // Logger. A newline is appended if the last character of s is not
127 // already a newline. Calldepth is used to recover the PC and is
128 // provided for generality, although at the moment on all pre-defined
129 // paths it will be 2.
130 func (l
*Logger
) Output(calldepth
int, s
string) error
{
131 now
:= time
.Now() // get this early.
136 if l
.flag
&(Lshortfile|Llongfile
) != 0 {
137 // release lock while getting caller info - it's expensive.
140 _
, file
, line
, ok
= runtime
.Caller(calldepth
)
148 l
.formatHeader(&l
.buf
, now
, file
, line
)
149 l
.buf
= append(l
.buf
, s
...)
150 if len(s
) > 0 && s
[len(s
)-1] != '\n' {
151 l
.buf
= append(l
.buf
, '\n')
153 _
, err
:= l
.out
.Write(l
.buf
)
157 // Printf calls l.Output to print to the logger.
158 // Arguments are handled in the manner of fmt.Printf.
159 func (l
*Logger
) Printf(format
string, v
...interface{}) {
160 l
.Output(2, fmt
.Sprintf(format
, v
...))
163 // Print calls l.Output to print to the logger.
164 // Arguments are handled in the manner of fmt.Print.
165 func (l
*Logger
) Print(v
...interface{}) { l
.Output(2, fmt
.Sprint(v
...)) }
167 // Println calls l.Output to print to the logger.
168 // Arguments are handled in the manner of fmt.Println.
169 func (l
*Logger
) Println(v
...interface{}) { l
.Output(2, fmt
.Sprintln(v
...)) }
171 // Fatal is equivalent to l.Print() followed by a call to os.Exit(1).
172 func (l
*Logger
) Fatal(v
...interface{}) {
173 l
.Output(2, fmt
.Sprint(v
...))
177 // Fatalf is equivalent to l.Printf() followed by a call to os.Exit(1).
178 func (l
*Logger
) Fatalf(format
string, v
...interface{}) {
179 l
.Output(2, fmt
.Sprintf(format
, v
...))
183 // Fatalln is equivalent to l.Println() followed by a call to os.Exit(1).
184 func (l
*Logger
) Fatalln(v
...interface{}) {
185 l
.Output(2, fmt
.Sprintln(v
...))
189 // Panic is equivalent to l.Print() followed by a call to panic().
190 func (l
*Logger
) Panic(v
...interface{}) {
191 s
:= fmt
.Sprint(v
...)
196 // Panicf is equivalent to l.Printf() followed by a call to panic().
197 func (l
*Logger
) Panicf(format
string, v
...interface{}) {
198 s
:= fmt
.Sprintf(format
, v
...)
203 // Panicln is equivalent to l.Println() followed by a call to panic().
204 func (l
*Logger
) Panicln(v
...interface{}) {
205 s
:= fmt
.Sprintln(v
...)
210 // Flags returns the output flags for the logger.
211 func (l
*Logger
) Flags() int {
217 // SetFlags sets the output flags for the logger.
218 func (l
*Logger
) SetFlags(flag
int) {
224 // Prefix returns the output prefix for the logger.
225 func (l
*Logger
) Prefix() string {
231 // SetPrefix sets the output prefix for the logger.
232 func (l
*Logger
) SetPrefix(prefix
string) {
238 // SetOutput sets the output destination for the standard logger.
239 func SetOutput(w io
.Writer
) {
241 defer std
.mu
.Unlock()
245 // Flags returns the output flags for the standard logger.
250 // SetFlags sets the output flags for the standard logger.
251 func SetFlags(flag
int) {
255 // Prefix returns the output prefix for the standard logger.
256 func Prefix() string {
260 // SetPrefix sets the output prefix for the standard logger.
261 func SetPrefix(prefix
string) {
262 std
.SetPrefix(prefix
)
265 // These functions write to the standard logger.
267 // Print calls Output to print to the standard logger.
268 // Arguments are handled in the manner of fmt.Print.
269 func Print(v
...interface{}) {
270 std
.Output(2, fmt
.Sprint(v
...))
273 // Printf calls Output to print to the standard logger.
274 // Arguments are handled in the manner of fmt.Printf.
275 func Printf(format
string, v
...interface{}) {
276 std
.Output(2, fmt
.Sprintf(format
, v
...))
279 // Println calls Output to print to the standard logger.
280 // Arguments are handled in the manner of fmt.Println.
281 func Println(v
...interface{}) {
282 std
.Output(2, fmt
.Sprintln(v
...))
285 // Fatal is equivalent to Print() followed by a call to os.Exit(1).
286 func Fatal(v
...interface{}) {
287 std
.Output(2, fmt
.Sprint(v
...))
291 // Fatalf is equivalent to Printf() followed by a call to os.Exit(1).
292 func Fatalf(format
string, v
...interface{}) {
293 std
.Output(2, fmt
.Sprintf(format
, v
...))
297 // Fatalln is equivalent to Println() followed by a call to os.Exit(1).
298 func Fatalln(v
...interface{}) {
299 std
.Output(2, fmt
.Sprintln(v
...))
303 // Panic is equivalent to Print() followed by a call to panic().
304 func Panic(v
...interface{}) {
305 s
:= fmt
.Sprint(v
...)
310 // Panicf is equivalent to Printf() followed by a call to panic().
311 func Panicf(format
string, v
...interface{}) {
312 s
:= fmt
.Sprintf(format
, v
...)
317 // Panicln is equivalent to Println() followed by a call to panic().
318 func Panicln(v
...interface{}) {
319 s
:= fmt
.Sprintln(v
...)