自定义golang中的log格式及输出位置, 以及json解析的2种常用方式
log
下面是log包一些常用的格式配置
1
2
3
4
5
6
7
8
9
|
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
LstdFlags = Ldate | Ltime // initial values for the standard logger
)
|
iota 在常量声明区有特殊的作用 参见
如下,定义了不同级别的log的格式及输出位置。
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
|
// This sample program demonstrates how to create customized loggers.
package main
import (
"io"
"io/ioutil"
"log"
"os"
)
var (
Trace *log.Logger // Just about anything
Info *log.Logger // Important information
Warning *log.Logger // Be concerned
Error *log.Logger // Critical problem
)
func init() {
file, err := os.OpenFile("errors.txt",
os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
log.Fatalln("Failed to open error log file:", err)
}
Trace = log.New(ioutil.Discard,
"TRACE: ",
log.Ldate|log.Ltime|log.Lshortfile)
Info = log.New(os.Stdout,
"INFO: ",
log.Ldate|log.Ltime|log.Lshortfile)
Warning = log.New(os.Stdout,
"WARNING: ",
log.Ldate|log.Ltime|log.Lshortfile)
Error = log.New(io.MultiWriter(file, os.Stderr),
"ERROR: ",
log.Ldate|log.Ltime|log.Lshortfile)
}
func main() {
Trace.Println("I have something standard to say")
Info.Println("Special Information")
Warning.Println("There is something you need to know about")
Error.Println("Something has failed")
}
|
json
第一种方式
使用 struct 来声明类型,使用tag来提供每个字段的元信息。如果不存在标签,编码和解码过程会试图以大小写无关的方式,直接使用字段的名字进行匹配。如果无法匹配,对应的结构类型 里的字段就包含其零值。
如下
1
2
3
4
5
6
7
8
|
type Contact struct {
Name string `json:"name"`
Title string `json:"title"`
Contact struct {
Home string `json:"home"`
Cell string `json:"cell"`
} `json:"contact"`
}
|
第二种方式
可以将 JSON 文档解码到一个 map 变量中,如下,将其中的结构类型变量替换为 map 类型的变 量。**变量 c 声明为一个 map 类型, 其键是 string 类型, 其值是 interface{} 类型。**这意味着这个 map 类型可以使用任意类型的值作为给定键的值。
Unmarshal
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
|
// This sample program demonstrates how to decode a JSON string.
package main
import (
"encoding/json"
"fmt"
"log"
)
// JSON contains a sample string to unmarshal.
var JSON = `{
"name": "Gopher",
"title": "programmer",
"contact": {
"home": "415.333.3333",
"cell": "415.555.5555"
}
}`
func main() {
// Unmarshal the JSON string into our map variable.
var c map[string]interface{}
err := json.Unmarshal([]byte(JSON), &c)
if err != nil {
log.Println("ERROR:", err)
return
}
fmt.Println("Name:", c["name"])
fmt.Println("Title:", c["title"])
fmt.Println("Contact")
fmt.Println("H:", c["contact"].(map[string]interface{})["home"])
fmt.Println("C:", c["contact"].(map[string]interface{})["cell"])
}
|
marshal
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
|
// This sample program demonstrates how to marshal a JSON string.
package main
import (
"encoding/json"
"fmt"
"log"
)
func main() {
// Create a map of key/value pairs.
c := make(map[string]interface{})
c["name"] = "Gopher"
c["title"] = "programmer"
c["contact"] = map[string]interface{}{
"home": "415.333.3333",
"cell": "415.555.5555",
}
// Marshal the map into a JSON string.
data, err := json.MarshalIndent(c, "", " ")
if err != nil {
log.Println("ERROR:", err)
return
}
fmt.Println(string(data))
}
|