123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- package log
- import (
- "flag"
- "fmt"
- "io"
- _log "log"
- "os"
- "strings"
- "source.uit.com/mikevoets/inf-3200-1/tools/log/config"
- "source.uit.com/mikevoets/inf-3200-1/tools/log/errors"
- )
- func log_file(p config.LogPrefix) *os.File {
- os.MkdirAll(config.LOG_DIR, 0777)
- f, err := os.OpenFile(
- fmt.Sprintf("%s%c%s%s.log", config.LOG_DIR, os.PathSeparator, strings.ToLower(string(p)), config.FILE_EXTRA),
- config.FILE_MASK,
- config.FILE_PERM,
- )
- if err != nil {
- _log.Fatal(err.Error())
- return nil
- }
- return f
- }
- func log_new(l config.LogLevel, h config.LogHandle, p config.LogPrefix) *_log.Logger {
- if (config.LOG_LEVEL_FILE&l) == config.LOG_LEVEL_FILE && (config.LOG_LEVEL_OUTPUT&l) == config.LOG_LEVEL_OUTPUT {
- f := log_file(p)
- return _log.New(
- io.MultiWriter(f, h),
- fmt.Sprintf("%s ", p),
- config.LOG_FORMAT,
- )
- }
- if (config.LOG_LEVEL_FILE & l) == config.LOG_LEVEL_FILE {
- f := log_file(p)
- return _log.New(
- f, fmt.Sprintf("%s ", p),
- config.LOG_FORMAT,
- )
- }
- if (config.LOG_LEVEL_OUTPUT & l) == config.LOG_LEVEL_OUTPUT {
- return _log.New(
- h, fmt.Sprintf("%s ", p),
- config.LOG_FORMAT,
- )
- }
- return _log.New(
- config.DISCARD_HANDLE, fmt.Sprintf("%s ", p),
- config.LOG_FORMAT,
- )
- }
- func init() {
- flag.Var(&config.LOG_LEVEL_OUTPUT, "llo", "Minimum log level that will be written to stdout.")
- flag.Var(&config.LOG_LEVEL_FILE, "llf", "Minimum log level that will be written to file")
- }
- func log(t config.LogLevel) *_log.Logger {
- var l *_log.Logger
- switch t {
- case config.TRACE:
- if config.Trace == nil {
- config.Trace = log_new(t, config.HANDLE_TRACE, config.PREFIX_TRACE)
- }
- l = config.Trace
- case config.INFO:
- if config.Info == nil {
- config.Info = log_new(t, config.HANDLE_INFO, config.PREFIX_INFO)
- }
- l = config.Info
- case config.WARNING:
- if config.Warning == nil {
- config.Warning = log_new(t, config.HANDLE_WARNING, config.PREFIX_WARNING)
- }
- l = config.Warning
- case config.ERROR:
- if config.Error == nil {
- config.Error = log_new(t, config.HANDLE_ERROR, config.PREFIX_ERROR)
- }
- l = config.Error
- case config.PANIC:
- if config.Panic == nil {
- config.Panic = log_new(t, config.HANDLE_ERROR, config.PREFIX_PANIC)
- }
- l = config.Panic
- }
- if l == nil {
- _log.Panic(errors.UNKNOWN_LOGGER.Error())
- }
- return l
- }
- func LLO() *config.LogLevel {
- return &config.LOG_LEVEL_OUTPUT
- }
- func LLF() *config.LogLevel {
- return &config.LOG_LEVEL_FILE
- }
- func Trace(m string) {
- log(config.TRACE).Print(m)
- }
- func Tracef(f string, v ...interface{}) {
- log(config.TRACE).Printf(f, v...)
- }
- func Traceln(v ...interface{}) {
- log(config.TRACE).Println(v...)
- }
- func Info(m string) {
- log(config.INFO).Print(m)
- }
- func Infof(f string, v ...interface{}) {
- log(config.INFO).Printf(f, v...)
- }
- func Infoln(v ...interface{}) {
- log(config.INFO).Println(v...)
- }
- func Warning(m string) {
- log(config.WARNING).Print(m)
- }
- func Warningf(f string, v ...interface{}) {
- log(config.WARNING).Printf(f, v...)
- }
- func Warningln(v ...interface{}) {
- log(config.WARNING).Println(v...)
- }
- func Error(m string) {
- log(config.ERROR).Fatal(m)
- }
- func Errorf(f string, v ...interface{}) {
- log(config.ERROR).Fatalf(f, v...)
- }
- func Errorln(v ...interface{}) {
- log(config.ERROR).Fatalln(v...)
- }
- func Panic(m string) {
- log(config.PANIC).Panic(m)
- }
- func Panicf(f string, v ...interface{}) {
- log(config.PANIC).Panicf(f, v...)
- }
- func Panicln(v ...interface{}) {
- log(config.PANIC).Panicln(v...)
- }
|