exported.go 6.2 KB
Newer Older
Y
Your Name 已提交
1
package gokulog
E
eoLinker API Management 已提交
2 3 4 5 6 7 8 9 10

import (
	"fmt"
	"github.com/sirupsen/logrus"
	"os"
	"time"
)

const (
Y
Your Name 已提交
11
	//PanicLevel panic
E
eoLinker API Management 已提交
12
	PanicLevel = logrus.PanicLevel
Y
Your Name 已提交
13
	//FatalLevel fatal
E
eoLinker API Management 已提交
14
	FatalLevel = logrus.FatalLevel
Y
Your Name 已提交
15
	//ErrorLevel error
E
eoLinker API Management 已提交
16
	ErrorLevel = logrus.ErrorLevel
Y
Your Name 已提交
17
	//WarnLevel warn
E
eoLinker API Management 已提交
18
	WarnLevel  = logrus.WarnLevel
Y
Your Name 已提交
19
	//InfoLevel info
E
eoLinker API Management 已提交
20
	InfoLevel  = logrus.InfoLevel
Y
Your Name 已提交
21
	//DebugLevel debug
E
eoLinker API Management 已提交
22
	DebugLevel = logrus.DebugLevel
Y
Your Name 已提交
23
	//TraceLevel trace
E
eoLinker API Management 已提交
24 25 26 27
	TraceLevel = logrus.TraceLevel
)

var (
Y
Your Name 已提交
28
	writer *FileWriterByPeriod
E
eoLinker API Management 已提交
29 30 31 32 33
	logger                     = logrus.New()

	logEnable = true
)

Y
Your Name 已提交
34
//Level level
E
eoLinker API Management 已提交
35
type Level = logrus.Level
Y
Your Name 已提交
36 37

//Fields fields
E
eoLinker API Management 已提交
38
type Fields = logrus.Fields
Y
Your Name 已提交
39 40

//Entry entry
E
eoLinker API Management 已提交
41 42
type Entry = logrus.Entry

Y
Your Name 已提交
43
//ParseLevel 转换层级
E
eoLinker API Management 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
func ParseLevel(lvl string) (Level, error) {
	return logrus.ParseLevel(lvl)
}

func init() {
	logger.SetLevel(logrus.WarnLevel)
	logger.SetFormatter(&LineFormatter{
		//ForceColors: true,
		//FullTimestamp:             true,
		TimestampFormat: "2006-01-02 15:04:05",
	})

	writer = NewFileWriteBytePeriod()
	logrus.RegisterExitHandler(func() {
		Close()
	})
}
Y
Your Name 已提交
61 62

//GetLogger 获取日志对象
E
eoLinker API Management 已提交
63 64 65
func GetLogger() *logrus.Logger {
	return logger
}
Y
Your Name 已提交
66 67

//SetLevel 设置日志层级
E
eoLinker API Management 已提交
68 69 70 71 72
func SetLevel(level Level) {

	logger.SetLevel(level)

}
Y
Your Name 已提交
73 74

//SetOutPut 输出设置
E
eoLinker API Management 已提交
75 76 77 78 79 80 81 82 83 84 85
func SetOutPut(enable bool, dir, file string, period LogPeriod, expire int) {
	logEnable = enable
	logger.SetOutput(writer)
	if enable {
		writer.Set(dir, file, period, time.Duration(expire)*time.Hour*24)
		writer.Open()
	} else {
		writer.Close()
	}
}

Y
Your Name 已提交
86
//Close 关闭流
E
eoLinker API Management 已提交
87 88 89
func Close() {
	writer.Close()
}
Y
Your Name 已提交
90 91

//WithFields 设置日志需要的字段名称
E
eoLinker API Management 已提交
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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
func WithFields(fields Fields) *Entry {

	return logger.WithFields(fields)
}

// Trace logs a message at level Trace on the standard logger.
func Trace(args ...interface{}) {
	if !logEnable {
		return
	}
	logger.Trace(args...)
}

// Debug logs a message at level Debug on the standard logger.
func Debug(args ...interface{}) {

	if !logEnable {
		return
	}
	logger.Debug(args...)
}

// Info logs a message at level Info on the standard logger.
func Info(args ...interface{}) {

	if !logEnable {
		return
	}
	logger.Info(args...)
}

// Warn logs a message at level Warn on the standard logger.
func Warn(args ...interface{}) {

	if !logEnable {
		return
	}
	logger.Warn(args...)
}

// Warning logs a message at level Warn on the standard logger.
func Warning(args ...interface{}) {

	if !logEnable {
		return
	}
	logger.Warning(args...)
}

// Error logs a message at level Error on the standard logger.
func Error(args ...interface{}) {

	if !logEnable {
		return
	}
	logger.Error(args...)
}
func panicout(args ...interface{}) string {
	defer func() {
		if e := recover(); e != nil {
			Close()
		}
	}()
	s, _ := encode(PanicLevel, args...)
	_, _ = os.Stderr.Write(s)
	logger.Panic(args...)
	return string(s)
}

// Panic logs a message at level Panic on the standard logger.
func Panic(args ...interface{}) {
	panic(panicout(args...))
}

// Fatal logs a message at level Fatal on the standard logger then the process will exit with status set to 1.
func Fatal(args ...interface{}) {
	s, e := encode(FatalLevel, args...)
	if e != nil {
		return
	}
	_, _ = os.Stderr.Write(s)
	logger.Fatal(args...)
}

// Tracef logs a message at level Trace on the standard logger.
func Tracef(format string, args ...interface{}) {

	if !logEnable {
		return
	}
	logger.Tracef(format, args...)
}

// Debugf logs a message at level Debug on the standard logger.
func Debugf(format string, args ...interface{}) {

	if !logEnable {
		return
	}
	logger.Debugf(format, args...)
}

// Infof logs a message at level Info on the standard logger.
func Infof(format string, args ...interface{}) {

	if !logEnable {
		return
	}
	logger.Infof(format, args...)
}

// Warnf logs a message at level Warn on the standard logger.
func Warnf(format string, args ...interface{}) {

	if !logEnable {
		return
	}
	logger.Warnf(format, args...)
}

// Warningf logs a message at level Warn on the standard logger.
func Warningf(format string, args ...interface{}) {

	if !logEnable {
		return
	}
	logger.Warningf(format, args...)
}

// Errorf logs a message at level Error on the standard logger.
func Errorf(format string, args ...interface{}) {

	if !logEnable {
		return
	}
	logger.Errorf(format, args...)
}

// Fatalf logs a message at level Fatal on the standard logger then the process will exit with status set to 1.
func Fatalf(format string, args ...interface{}) {

	s, e := encode(FatalLevel, fmt.Sprintf(format, args...))
	if e != nil {
		return
	}
	_, _ = os.Stdout.Write(s)
	logger.Fatalf(format, args...)

}

// Traceln logs a message at level Trace on the standard logger.
func Traceln(args ...interface{}) {

	if !logEnable {
		return
	}
	logger.Traceln(args...)
}

// Debugln logs a message at level Debug on the standard logger.
func Debugln(args ...interface{}) {

	if !logEnable {
		return
	}
	logger.Debugln(args...)
}

Y
Your Name 已提交
260
// Print 无视lever输出一段trace 信息
E
eoLinker API Management 已提交
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
func Print(args ...interface{}) {

	s, e := encode(TraceLevel, args...)
	if e != nil {
		return
	}
	if len(s) == 0 {
		return
	}

	if logEnable {
		writer.Write(s)
	} else {
		os.Stdout.Write(s)
	}
}

func encode(level Level, args ...interface{}) ([]byte, error) {
	entry := logrus.NewEntry(logger)
	entry.Message = fmt.Sprintln(args...)
	entry.Level = level
	entry.Time = time.Now()
	s, e := logger.Formatter.Format(entry)
	if e != nil {
		fmt.Println(e)
		fmt.Println(args...)
	}
	return s, e
}

// Infoln logs a message at level Info on the standard logger.
func Infoln(args ...interface{}) {

	if !logEnable {
		return
	}
	logger.Infoln(args...)
}

// Warnln logs a message at level Warn on the standard logger.
func Warnln(args ...interface{}) {

	if !logEnable {
		return
	}
	logger.Warnln(args...)
}

// Warningln logs a message at level Warn on the standard logger.
func Warningln(args ...interface{}) {

	if !logEnable {
		return
	}
	logger.Warningln(args...)
}

// Errorln logs a message at level Error on the standard logger.
func Errorln(args ...interface{}) {

	if !logEnable {
		return
	}
	logger.Errorln(args...)
}

// Fatalln logs a message at level Fatal on the standard logger then the process will exit with status set to 1.
func Fatalln(args ...interface{}) {

	s, e := encode(FatalLevel, args...)
	if e != nil {
		return
	}
	_, _ = os.Stderr.Write(s)
	logger.Fatalln(args...)
}