forked from ardanlabs/gotraining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample2.go
83 lines (65 loc) · 2.07 KB
/
example2.go
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
// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
/*
func New(out io.Writer, prefix string, flag int) *Logger
out: The out variable sets the destination to which log data will be written.
prefix: The prefix appears at the beginning of each generated log line.
flags: The flag argument defines the logging properties.
*/
// Sample program to show how to extend the log package
// from the standard library.
package main
import (
"io"
"io/ioutil"
"log"
"os"
)
var (
// Trace is for full detailed messages.
Trace *log.Logger
// Info is for important messages.
Info *log.Logger
// Warning is for need to know issue messages.
Warning *log.Logger
// Error is for error messages.
Error *log.Logger
)
// initLog sets the devices for each log type.
func initLog(traceHandle io.Writer, infoHandle io.Writer, warningHandle io.Writer, errorHandle io.Writer) {
Trace = log.New(traceHandle,
"TRACE: ",
log.Ldate|log.Ltime|log.Lshortfile)
Info = log.New(infoHandle,
"INFO: ",
log.Ldate|log.Ltime|log.Lshortfile)
Warning = log.New(warningHandle,
"WARNING: ",
log.Ldate|log.Ltime|log.Lshortfile)
Error = log.New(errorHandle,
"ERROR: ",
log.Ldate|log.Ltime|log.Lshortfile)
}
func main() {
// Open a file for warnings.
warnings, err := os.OpenFile("warnings.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
log.Fatalln("Failed to open warning log file")
}
defer warnings.Close()
// Open a file for errors.
errors, err := os.OpenFile("errors.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
log.Fatalln("Failed to open errors log file")
}
defer errors.Close()
// Create a multi writer for errors.
multi := io.MultiWriter(errors, os.Stderr)
// Init the log package for each message type.
initLog(ioutil.Discard, os.Stdout, warnings, multi)
// Test each log type.
Trace.Println("I have something standard to say.")
Info.Println("Important Information.")
Warning.Println("There is something you need to know about.")
Error.Println("Something has failed.")
}