studygolang /websites/code/studygolang/src/logger/logger.go

Language Go Lines 72
MD5 Hash cf25027f97b96eaf1f4bb7f4c0d0df1f
Repository https://github.com/xiedantibu/studygolang.git View Raw File
 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
// Copyright 2013 The StudyGolang Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// http://studygolang.com
// Authorpolaris	studygolang@gmail.com

package logger

import (
	"config"
	"io"
	"log"
	"os"
)

var (
	// 日志文件
	info_file  = config.ROOT + "/log/info.log"
	debug_file = config.ROOT + "/log/debug.log"
	error_file = config.ROOT + "/log/error.log"
)

func init() {
	os.Mkdir(config.ROOT+"/log/", 0777)
}

type logger struct {
	*log.Logger
}

func Infof(format string, args ...interface{}) {
	file, err := os.OpenFile(info_file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0777)
	if err != nil {
		return
	}
	defer file.Close()
	New(file).Printf(format, args...)
}

func Infoln(args ...interface{}) {
	file, err := os.OpenFile(info_file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0777)
	if err != nil {
		return
	}
	defer file.Close()
	New(file).Println(args...)
}

func Errorf(format string, args ...interface{}) {
	file, err := os.OpenFile(error_file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0777)
	if err != nil {
		return
	}
	defer file.Close()
	New(file).Printf(format, args...)
}

func Errorln(args ...interface{}) {
	file, err := os.OpenFile(error_file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0777)
	if err != nil {
		return
	}
	defer file.Close()
	New(file).Println(args...)
}

func New(out io.Writer) *logger {
	return &logger{
		Logger: log.New(out, "", log.LstdFlags),
	}
}
Back to Top