-
Notifications
You must be signed in to change notification settings - Fork 2
/
console.go
136 lines (112 loc) · 3.47 KB
/
console.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
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
// MIT License
// Copyright (c) 2019 gonethopper
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// * @Author: ankye
// * @Date: 2019-06-24 13:35:23
// * @Last Modified by: ankye
// * @Last Modified time: 2019-06-24 13:35:23
package log
import (
"fmt"
"io"
"os"
"sync/atomic"
)
// NewConsoleLogger create FileLog instance
func NewConsoleLogger(conf *Config) (ILog, error) {
logger := &ConsoleLog{}
if err := logger.ParseConfig(conf); err != nil {
return nil, err
}
if err := logger.InitLogger(conf); err != nil {
return nil, err
}
return logger, nil
}
// ConsoleLog writes messages to terminal.
type ConsoleLog struct {
//set level and atomic incr CurrentSize and CurrentLines
level int32
writer io.Writer
Conf *Config
}
// ParseConfig read config from map[string]interface{}
// config key map
// level default 7
func (c *ConsoleLog) ParseConfig(conf *Config) error {
return nil
}
// InitLogger init logger
func (c *ConsoleLog) InitLogger(conf *Config) error {
c.Conf = conf
c.writer = os.Stdout
return nil
}
// SetLevel atomic set level
func (c *ConsoleLog) SetLevel(level int32) error {
if level < FATAL || level > DEBUG {
return fmt.Errorf("log level:[%d] invalid", level)
}
atomic.StoreInt32(&c.level, level)
return nil
}
// GetLevel atomic get level
func (c *ConsoleLog) GetLevel() int32 {
level := atomic.LoadInt32(&c.level)
return level
}
// WriteLog write log to file, return immediately if not meet the conditions
func (c *ConsoleLog) WriteLog(msg []byte, count int32) error {
c.writer.Write(msg)
return nil
}
// CanLog check log status
func (c *ConsoleLog) CanLog(msgSize int32, count int32) bool {
return true
}
// Close and flush
func (c *ConsoleLog) Close() error {
return nil
}
// PushLog push log to queue
func (c *ConsoleLog) PushLog(level int32, v ...interface{}) error {
if level > c.level {
return nil
}
msg := FormatLog(level, v...)
return c.WriteLog([]byte(msg), 1)
}
// Fatal system is unusable
func (c *ConsoleLog) Fatal(v ...interface{}) error {
return c.PushLog(FATAL, v...)
}
// Error error conditions
func (c *ConsoleLog) Error(v ...interface{}) error {
return c.PushLog(ERROR, v...)
}
// Warning warning conditions
func (c *ConsoleLog) Warning(v ...interface{}) error {
return c.PushLog(WARNING, v...)
}
// Info informational messages
func (c *ConsoleLog) Info(v ...interface{}) error {
return c.PushLog(INFO, v...)
}
// Debug debug-level messages
func (c *ConsoleLog) Debug(v ...interface{}) error {
return c.PushLog(DEBUG, v...)
}