1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
<h1> Package log </h1> <ul id="short-nav">
<li><code>import "log"</code></li>
<li><a href="#pkg-overview" class="overviewLink">Overview</a></li>
<li><a href="#pkg-index" class="indexLink">Index</a></li>
<li><a href="#pkg-examples" class="examplesLink">Examples</a></li>
<li><a href="#pkg-subdirectories">Subdirectories</a></li>
</ul> <h2 id="pkg-overview">Overview </h2> <p>Package log implements a simple logging package. It defines a type, <a href="#Logger">Logger</a>, with methods for formatting output. It also has a predefined 'standard' Logger accessible through helper functions Print[f|ln], Fatal[f|ln], and Panic[f|ln], which are easier to use than creating a Logger manually. That logger writes to standard error and prints the date and time of each logged message. Every log message is output on a separate line: if the message being printed does not end in a newline, the logger will add one. The Fatal functions call <span>os.Exit</span>(1) after writing the log message. The Panic functions call panic after writing the log message. </p> <h2 id="pkg-index">Index </h2> <ul id="manual-nav">
<li><a href="#pkg-constants">Constants</a></li>
<li><a href="#Fatal">func Fatal(v ...any)</a></li>
<li><a href="#Fatalf">func Fatalf(format string, v ...any)</a></li>
<li><a href="#Fatalln">func Fatalln(v ...any)</a></li>
<li><a href="#Flags">func Flags() int</a></li>
<li><a href="#Output">func Output(calldepth int, s string) error</a></li>
<li><a href="#Panic">func Panic(v ...any)</a></li>
<li><a href="#Panicf">func Panicf(format string, v ...any)</a></li>
<li><a href="#Panicln">func Panicln(v ...any)</a></li>
<li><a href="#Prefix">func Prefix() string</a></li>
<li><a href="#Print">func Print(v ...any)</a></li>
<li><a href="#Printf">func Printf(format string, v ...any)</a></li>
<li><a href="#Println">func Println(v ...any)</a></li>
<li><a href="#SetFlags">func SetFlags(flag int)</a></li>
<li><a href="#SetOutput">func SetOutput(w io.Writer)</a></li>
<li><a href="#SetPrefix">func SetPrefix(prefix string)</a></li>
<li><a href="#Writer">func Writer() io.Writer</a></li>
<li><a href="#Logger">type Logger</a></li>
<li> <a href="#Default">func Default() *Logger</a>
</li>
<li> <a href="#New">func New(out io.Writer, prefix string, flag int) *Logger</a>
</li>
<li> <a href="#Logger.Fatal">func (l *Logger) Fatal(v ...any)</a>
</li>
<li> <a href="#Logger.Fatalf">func (l *Logger) Fatalf(format string, v ...any)</a>
</li>
<li> <a href="#Logger.Fatalln">func (l *Logger) Fatalln(v ...any)</a>
</li>
<li> <a href="#Logger.Flags">func (l *Logger) Flags() int</a>
</li>
<li> <a href="#Logger.Output">func (l *Logger) Output(calldepth int, s string) error</a>
</li>
<li> <a href="#Logger.Panic">func (l *Logger) Panic(v ...any)</a>
</li>
<li> <a href="#Logger.Panicf">func (l *Logger) Panicf(format string, v ...any)</a>
</li>
<li> <a href="#Logger.Panicln">func (l *Logger) Panicln(v ...any)</a>
</li>
<li> <a href="#Logger.Prefix">func (l *Logger) Prefix() string</a>
</li>
<li> <a href="#Logger.Print">func (l *Logger) Print(v ...any)</a>
</li>
<li> <a href="#Logger.Printf">func (l *Logger) Printf(format string, v ...any)</a>
</li>
<li> <a href="#Logger.Println">func (l *Logger) Println(v ...any)</a>
</li>
<li> <a href="#Logger.SetFlags">func (l *Logger) SetFlags(flag int)</a>
</li>
<li> <a href="#Logger.SetOutput">func (l *Logger) SetOutput(w io.Writer)</a>
</li>
<li> <a href="#Logger.SetPrefix">func (l *Logger) SetPrefix(prefix string)</a>
</li>
<li> <a href="#Logger.Writer">func (l *Logger) Writer() io.Writer</a>
</li>
</ul> <div id="pkg-examples"> <h3>Examples</h3> <dl> <dd><a class="exampleLink" href="#example_Logger">Logger</a></dd> <dd><a class="exampleLink" href="#example_Logger_Output">Logger.Output</a></dd> </dl> </div> <h3>Package files</h3> <p> <span>log.go</span> </p> <h2 id="pkg-constants">Constants</h2> <p>These flags define which text to prefix to each log entry generated by the <a href="#Logger">Logger</a>. Bits are or'ed together to control what's printed. With the exception of the Lmsgprefix flag, there is no control over the order they appear (the order listed here) or the format they present (as described in the comments). The prefix is followed by a colon only when Llongfile or Lshortfile is specified. For example, flags Ldate | Ltime (or LstdFlags) produce, </p>
<pre data-language="go">2009/01/23 01:23:23 message
</pre> <p>while flags Ldate | Ltime | Lmicroseconds | Llongfile produce, </p>
<pre data-language="go">2009/01/23 01:23:23.123123 /a/b/c/d.go:23: message
</pre> <pre data-language="go">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
)</pre> <h2 id="Fatal">func <span>Fatal</span> </h2> <pre data-language="go">func Fatal(v ...any)</pre> <p>Fatal is equivalent to <a href="#Print">Print</a> followed by a call to <span>os.Exit</span>(1). </p>
<h2 id="Fatalf">func <span>Fatalf</span> </h2> <pre data-language="go">func Fatalf(format string, v ...any)</pre> <p>Fatalf is equivalent to <a href="#Printf">Printf</a> followed by a call to <span>os.Exit</span>(1). </p>
<h2 id="Fatalln">func <span>Fatalln</span> </h2> <pre data-language="go">func Fatalln(v ...any)</pre> <p>Fatalln is equivalent to <a href="#Println">Println</a> followed by a call to <span>os.Exit</span>(1). </p>
<h2 id="Flags">func <span>Flags</span> </h2> <pre data-language="go">func Flags() int</pre> <p>Flags returns the output flags for the standard logger. The flag bits are <a href="#Ldate">Ldate</a>, <a href="#Ltime">Ltime</a>, and so on. </p>
<h2 id="Output">func <span>Output</span> <span title="Added in Go 1.5">1.5</span> </h2> <pre data-language="go">func Output(calldepth int, s string) error</pre> <p>Output writes the output for a logging event. The string s contains the text to print after the prefix specified by the flags of the Logger. A newline is appended if the last character of s is not already a newline. Calldepth is the count of the number of frames to skip when computing the file name and line number if <a href="#Llongfile">Llongfile</a> or <a href="#Lshortfile">Lshortfile</a> is set; a value of 1 will print the details for the caller of Output. </p>
<h2 id="Panic">func <span>Panic</span> </h2> <pre data-language="go">func Panic(v ...any)</pre> <p>Panic is equivalent to <a href="#Print">Print</a> followed by a call to panic(). </p>
<h2 id="Panicf">func <span>Panicf</span> </h2> <pre data-language="go">func Panicf(format string, v ...any)</pre> <p>Panicf is equivalent to <a href="#Printf">Printf</a> followed by a call to panic(). </p>
<h2 id="Panicln">func <span>Panicln</span> </h2> <pre data-language="go">func Panicln(v ...any)</pre> <p>Panicln is equivalent to <a href="#Println">Println</a> followed by a call to panic(). </p>
<h2 id="Prefix">func <span>Prefix</span> </h2> <pre data-language="go">func Prefix() string</pre> <p>Prefix returns the output prefix for the standard logger. </p>
<h2 id="Print">func <span>Print</span> </h2> <pre data-language="go">func Print(v ...any)</pre> <p>Print calls Output to print to the standard logger. Arguments are handled in the manner of <span>fmt.Print</span>. </p>
<h2 id="Printf">func <span>Printf</span> </h2> <pre data-language="go">func Printf(format string, v ...any)</pre> <p>Printf calls Output to print to the standard logger. Arguments are handled in the manner of <span>fmt.Printf</span>. </p>
<h2 id="Println">func <span>Println</span> </h2> <pre data-language="go">func Println(v ...any)</pre> <p>Println calls Output to print to the standard logger. Arguments are handled in the manner of <span>fmt.Println</span>. </p>
<h2 id="SetFlags">func <span>SetFlags</span> </h2> <pre data-language="go">func SetFlags(flag int)</pre> <p>SetFlags sets the output flags for the standard logger. The flag bits are <a href="#Ldate">Ldate</a>, <a href="#Ltime">Ltime</a>, and so on. </p>
<h2 id="SetOutput">func <span>SetOutput</span> </h2> <pre data-language="go">func SetOutput(w io.Writer)</pre> <p>SetOutput sets the output destination for the standard logger. </p>
<h2 id="SetPrefix">func <span>SetPrefix</span> </h2> <pre data-language="go">func SetPrefix(prefix string)</pre> <p>SetPrefix sets the output prefix for the standard logger. </p>
<h2 id="Writer">func <span>Writer</span> <span title="Added in Go 1.13">1.13</span> </h2> <pre data-language="go">func Writer() io.Writer</pre> <p>Writer returns the output destination for the standard logger. </p>
<h2 id="Logger">type <span>Logger</span> </h2> <p>A Logger represents an active logging object that generates lines of output to an <span>io.Writer</span>. Each logging operation makes a single call to the Writer's Write method. A Logger can be used simultaneously from multiple goroutines; it guarantees to serialize access to the Writer. </p>
<pre data-language="go">type Logger struct {
// contains filtered or unexported fields
}
</pre> <h4 id="example_Logger"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">var (
buf bytes.Buffer
logger = log.New(&buf, "logger: ", log.Lshortfile)
)
logger.Print("Hello, log file!")
fmt.Print(&buf)
</pre> <p>Output:</p> <pre class="output" data-language="go">logger: example_test.go:19: Hello, log file!
</pre> <h3 id="Default">func <span>Default</span> <span title="Added in Go 1.16">1.16</span> </h3> <pre data-language="go">func Default() *Logger</pre> <p>Default returns the standard logger used by the package-level output functions. </p>
<h3 id="New">func <span>New</span> </h3> <pre data-language="go">func New(out io.Writer, prefix string, flag int) *Logger</pre> <p>New creates a new <a href="#Logger">Logger</a>. The out variable sets the destination to which log data will be written. The prefix appears at the beginning of each generated log line, or after the log header if the <a href="#Lmsgprefix">Lmsgprefix</a> flag is provided. The flag argument defines the logging properties. </p>
<h3 id="Logger.Fatal">func (*Logger) <span>Fatal</span> </h3> <pre data-language="go">func (l *Logger) Fatal(v ...any)</pre> <p>Fatal is equivalent to l.Print() followed by a call to <span>os.Exit</span>(1). </p>
<h3 id="Logger.Fatalf">func (*Logger) <span>Fatalf</span> </h3> <pre data-language="go">func (l *Logger) Fatalf(format string, v ...any)</pre> <p>Fatalf is equivalent to l.Printf() followed by a call to <span>os.Exit</span>(1). </p>
<h3 id="Logger.Fatalln">func (*Logger) <span>Fatalln</span> </h3> <pre data-language="go">func (l *Logger) Fatalln(v ...any)</pre> <p>Fatalln is equivalent to l.Println() followed by a call to <span>os.Exit</span>(1). </p>
<h3 id="Logger.Flags">func (*Logger) <span>Flags</span> </h3> <pre data-language="go">func (l *Logger) Flags() int</pre> <p>Flags returns the output flags for the logger. The flag bits are <a href="#Ldate">Ldate</a>, <a href="#Ltime">Ltime</a>, and so on. </p>
<h3 id="Logger.Output">func (*Logger) <span>Output</span> </h3> <pre data-language="go">func (l *Logger) Output(calldepth int, s string) error</pre> <p>Output writes the output for a logging event. The string s contains the text to print after the prefix specified by the flags of the Logger. A newline is appended if the last character of s is not already a newline. Calldepth is used to recover the PC and is provided for generality, although at the moment on all pre-defined paths it will be 2. </p> <h4 id="example_Logger_Output"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">var (
buf bytes.Buffer
logger = log.New(&buf, "INFO: ", log.Lshortfile)
infof = func(info string) {
logger.Output(2, info)
}
)
infof("Hello world")
fmt.Print(&buf)
</pre> <p>Output:</p> <pre class="output" data-language="go">INFO: example_test.go:36: Hello world
</pre> <h3 id="Logger.Panic">func (*Logger) <span>Panic</span> </h3> <pre data-language="go">func (l *Logger) Panic(v ...any)</pre> <p>Panic is equivalent to l.Print() followed by a call to panic(). </p>
<h3 id="Logger.Panicf">func (*Logger) <span>Panicf</span> </h3> <pre data-language="go">func (l *Logger) Panicf(format string, v ...any)</pre> <p>Panicf is equivalent to l.Printf() followed by a call to panic(). </p>
<h3 id="Logger.Panicln">func (*Logger) <span>Panicln</span> </h3> <pre data-language="go">func (l *Logger) Panicln(v ...any)</pre> <p>Panicln is equivalent to l.Println() followed by a call to panic(). </p>
<h3 id="Logger.Prefix">func (*Logger) <span>Prefix</span> </h3> <pre data-language="go">func (l *Logger) Prefix() string</pre> <p>Prefix returns the output prefix for the logger. </p>
<h3 id="Logger.Print">func (*Logger) <span>Print</span> </h3> <pre data-language="go">func (l *Logger) Print(v ...any)</pre> <p>Print calls l.Output to print to the logger. Arguments are handled in the manner of <span>fmt.Print</span>. </p>
<h3 id="Logger.Printf">func (*Logger) <span>Printf</span> </h3> <pre data-language="go">func (l *Logger) Printf(format string, v ...any)</pre> <p>Printf calls l.Output to print to the logger. Arguments are handled in the manner of <span>fmt.Printf</span>. </p>
<h3 id="Logger.Println">func (*Logger) <span>Println</span> </h3> <pre data-language="go">func (l *Logger) Println(v ...any)</pre> <p>Println calls l.Output to print to the logger. Arguments are handled in the manner of <span>fmt.Println</span>. </p>
<h3 id="Logger.SetFlags">func (*Logger) <span>SetFlags</span> </h3> <pre data-language="go">func (l *Logger) SetFlags(flag int)</pre> <p>SetFlags sets the output flags for the logger. The flag bits are <a href="#Ldate">Ldate</a>, <a href="#Ltime">Ltime</a>, and so on. </p>
<h3 id="Logger.SetOutput">func (*Logger) <span>SetOutput</span> <span title="Added in Go 1.5">1.5</span> </h3> <pre data-language="go">func (l *Logger) SetOutput(w io.Writer)</pre> <p>SetOutput sets the output destination for the logger. </p>
<h3 id="Logger.SetPrefix">func (*Logger) <span>SetPrefix</span> </h3> <pre data-language="go">func (l *Logger) SetPrefix(prefix string)</pre> <p>SetPrefix sets the output prefix for the logger. </p>
<h3 id="Logger.Writer">func (*Logger) <span>Writer</span> <span title="Added in Go 1.12">1.12</span> </h3> <pre data-language="go">func (l *Logger) Writer() io.Writer</pre> <p>Writer returns the output destination for the logger. </p>
<h2 id="pkg-subdirectories">Subdirectories</h2> <div class="pkg-dir"> <table> <tr> <th class="pkg-name">Name</th> <th class="pkg-synopsis">Synopsis</th> </tr> <tr> <td colspan="2"><a href="../index">..</a></td> </tr> <tr> <td class="pkg-name"> <a href="slog/index">slog</a> </td> <td class="pkg-synopsis"> Package slog provides structured logging, in which log records include a message, a severity level, and various other attributes expressed as key-value pairs. </td> </tr> <tr> <td class="pkg-name"> <a href="syslog/index">syslog</a> </td> <td class="pkg-synopsis"> Package syslog provides a simple interface to the system log service. </td> </tr> </table> </div><div class="_attribution">
<p class="_attribution-p">
© Google, Inc.<br>Licensed under the Creative Commons Attribution License 3.0.<br>
<a href="http://golang.org/pkg/log/" class="_attribution-link">http://golang.org/pkg/log/</a>
</p>
</div>
|