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 // Every log message is output on a separate line: if the message being
12 // printed does not end in a newline, the logger will add one.
13 // The Fatal functions call os.Exit(1) after writing the log message.
14 // The Panic functions call panic after writing the log message.
26 // These flags define which text to prefix to each log entry generated by the Logger.
27 // Bits are or'ed together to control what's printed.
28 // There is no control over the order they appear (the order listed
29 // here) or the format they present (as described in the comments).
30 // The prefix is followed by a colon only when Llongfile or Lshortfile
32 // For example, flags Ldate | Ltime (or LstdFlags) produce,
33 // 2009/01/23 01:23:23 message
34 // while flags Ldate | Ltime | Lmicroseconds | Llongfile produce,
35 // 2009/01/23 01:23:23.123123 /a/b/c/d.go:23: message
37 Ldate = 1 << iota // the date in the local time zone: 2009/01/23
38 Ltime // the time in the local time zone: 01:23:23
39 Lmicroseconds // microsecond resolution: 01:23:23.123123. assumes Ltime.
40 Llongfile // full file name and line number: /a/b/c/d.go:23
41 Lshortfile // final file name element and line number: d.go:23. overrides Llongfile
42 LUTC // if Ldate or Ltime is set, use UTC rather than the local time zone
43 LstdFlags = Ldate | Ltime // initial values for the standard logger
46 // A Logger represents an active logging object that generates lines of
47 // output to an io.Writer. Each logging operation makes a single call to
48 // the Writer's Write method. A Logger can be used simultaneously from
49 // multiple goroutines; it guarantees to serialize access to the Writer.
51 mu sync.Mutex // ensures atomic writes; protects the following fields
52 prefix string // prefix to write at beginning of each line
53 flag int // properties
54 out io.Writer // destination for output
55 buf []byte // for accumulating text to write
58 // New creates a new Logger. The out variable sets the
59 // destination to which log data will be written.
60 // The prefix appears at the beginning of each generated log line.
61 // The flag argument defines the logging properties.
62 func New(out io.Writer, prefix string, flag int) *Logger {
63 return &Logger{out: out, prefix: prefix, flag: flag}
66 // SetOutput sets the output destination for the logger.
67 func (l *Logger) SetOutput(w io.Writer) {
73 var std = New(os.Stderr, "", LstdFlags)
75 // Cheap integer to fixed-width decimal ASCII. Give a negative width to avoid zero-padding.
76 func itoa(buf *[]byte, i int, wid int) {
77 // Assemble decimal in reverse order.
80 for i >= 10 || wid > 1 {
83 b[bp] = byte('0' + i - q*10)
89 *buf = append(*buf, b[bp:]...)
92 // formatHeader writes log header to buf in following order:
93 // * l.prefix (if it's not blank),
94 // * date and/or time (if corresponding flags are provided),
95 // * file and line number (if corresponding flags are provided).
96 func (l *Logger) formatHeader(buf *[]byte, t time.Time, file string, line int) {
97 *buf = append(*buf, l.prefix...)
98 if l.flag&(Ldate|Ltime|Lmicroseconds) != 0 {
102 if l.flag&Ldate != 0 {
103 year, month, day := t.Date()
105 *buf = append(*buf, '/')
106 itoa(buf, int(month), 2)
107 *buf = append(*buf, '/')
109 *buf = append(*buf, ' ')
111 if l.flag&(Ltime|Lmicroseconds) != 0 {
112 hour, min, sec := t.Clock()
114 *buf = append(*buf, ':')
116 *buf = append(*buf, ':')
118 if l.flag&Lmicroseconds != 0 {
119 *buf = append(*buf, '.')
120 itoa(buf, t.Nanosecond()/1e3, 6)
122 *buf = append(*buf, ' ')
125 if l.flag&(Lshortfile|Llongfile) != 0 {
126 if l.flag&Lshortfile != 0 {
128 for i := len(file) - 1; i > 0; i-- {
136 *buf = append(*buf, file...)
137 *buf = append(*buf, ':')
139 *buf = append(*buf, ": "...)
143 // Output writes the output for a logging event. The string s contains
144 // the text to print after the prefix specified by the flags of the
145 // Logger. A newline is appended if the last character of s is not
146 // already a newline. Calldepth is used to recover the PC and is
147 // provided for generality, although at the moment on all pre-defined
148 // paths it will be 2.
149 func (l *Logger) Output(calldepth int, s string) error {
150 now := time.Now() // get this early.
155 if l.flag&(Lshortfile|Llongfile) != 0 {
156 // Release lock while getting caller info - it's expensive.
159 _, file, line, ok = runtime.Caller(calldepth)
167 l.formatHeader(&l.buf, now, file, line)
168 l.buf = append(l.buf, s...)
169 if len(s) == 0 || s[len(s)-1] != '\n' {
170 l.buf = append(l.buf, '\n')
172 _, err := l.out.Write(l.buf)
176 // Printf calls l.Output to print to the logger.
177 // Arguments are handled in the manner of fmt.Printf.
178 func (l *Logger) Printf(format string, v ...interface{}) {
179 l.Output(2, fmt.Sprintf(format, v...))
182 // Print calls l.Output to print to the logger.
183 // Arguments are handled in the manner of fmt.Print.
184 func (l *Logger) Print(v ...interface{}) { l.Output(2, fmt.Sprint(v...)) }
186 // Println calls l.Output to print to the logger.
187 // Arguments are handled in the manner of fmt.Println.
188 func (l *Logger) Println(v ...interface{}) { l.Output(2, fmt.Sprintln(v...)) }
190 // Fatal is equivalent to l.Print() followed by a call to os.Exit(1).
191 func (l *Logger) Fatal(v ...interface{}) {
192 l.Output(2, fmt.Sprint(v...))
196 // Fatalf is equivalent to l.Printf() followed by a call to os.Exit(1).
197 func (l *Logger) Fatalf(format string, v ...interface{}) {
198 l.Output(2, fmt.Sprintf(format, v...))
202 // Fatalln is equivalent to l.Println() followed by a call to os.Exit(1).
203 func (l *Logger) Fatalln(v ...interface{}) {
204 l.Output(2, fmt.Sprintln(v...))
208 // Panic is equivalent to l.Print() followed by a call to panic().
209 func (l *Logger) Panic(v ...interface{}) {
210 s := fmt.Sprint(v...)
215 // Panicf is equivalent to l.Printf() followed by a call to panic().
216 func (l *Logger) Panicf(format string, v ...interface{}) {
217 s := fmt.Sprintf(format, v...)
222 // Panicln is equivalent to l.Println() followed by a call to panic().
223 func (l *Logger) Panicln(v ...interface{}) {
224 s := fmt.Sprintln(v...)
229 // Flags returns the output flags for the logger.
230 func (l *Logger) Flags() int {
236 // SetFlags sets the output flags for the logger.
237 func (l *Logger) SetFlags(flag int) {
243 // Prefix returns the output prefix for the logger.
244 func (l *Logger) Prefix() string {
250 // SetPrefix sets the output prefix for the logger.
251 func (l *Logger) SetPrefix(prefix string) {
257 // Writer returns the output destination for the logger.
258 func (l *Logger) Writer() io.Writer {
264 // SetOutput sets the output destination for the standard logger.
265 func SetOutput(w io.Writer) {
267 defer std.mu.Unlock()
271 // Flags returns the output flags for the standard logger.
276 // SetFlags sets the output flags for the standard logger.
277 func SetFlags(flag int) {
281 // Prefix returns the output prefix for the standard logger.
282 func Prefix() string {
286 // SetPrefix sets the output prefix for the standard logger.
287 func SetPrefix(prefix string) {
288 std.SetPrefix(prefix)
291 // These functions write to the standard logger.
293 // Print calls Output to print to the standard logger.
294 // Arguments are handled in the manner of fmt.Print.
295 func Print(v ...interface{}) {
296 std.Output(2, fmt.Sprint(v...))
299 // Printf calls Output to print to the standard logger.
300 // Arguments are handled in the manner of fmt.Printf.
301 func Printf(format string, v ...interface{}) {
302 std.Output(2, fmt.Sprintf(format, v...))
305 // Println calls Output to print to the standard logger.
306 // Arguments are handled in the manner of fmt.Println.
307 func Println(v ...interface{}) {
308 std.Output(2, fmt.Sprintln(v...))
311 // Fatal is equivalent to Print() followed by a call to os.Exit(1).
312 func Fatal(v ...interface{}) {
313 std.Output(2, fmt.Sprint(v...))
317 // Fatalf is equivalent to Printf() followed by a call to os.Exit(1).
318 func Fatalf(format string, v ...interface{}) {
319 std.Output(2, fmt.Sprintf(format, v...))
323 // Fatalln is equivalent to Println() followed by a call to os.Exit(1).
324 func Fatalln(v ...interface{}) {
325 std.Output(2, fmt.Sprintln(v...))
329 // Panic is equivalent to Print() followed by a call to panic().
330 func Panic(v ...interface{}) {
331 s := fmt.Sprint(v...)
336 // Panicf is equivalent to Printf() followed by a call to panic().
337 func Panicf(format string, v ...interface{}) {
338 s := fmt.Sprintf(format, v...)
343 // Panicln is equivalent to Println() followed by a call to panic().
344 func Panicln(v ...interface{}) {
345 s := fmt.Sprintln(v...)
350 // Output writes the output for a logging event. The string s contains
351 // the text to print after the prefix specified by the flags of the
352 // Logger. A newline is appended if the last character of s is not
353 // already a newline. Calldepth is the count of the number of
354 // frames to skip when computing the file name and line number
355 // if Llongfile or Lshortfile is set; a value of 1 will print the details
356 // for the caller of Output.
357 func Output(calldepth int, s string) error {
358 return std.Output(calldepth+1, s) // +1 for this frame.