Skip to content
This repository has been archived by the owner on Jan 17, 2024. It is now read-only.

Commit

Permalink
Merge pull request #29 from mylockerteam/develop
Browse files Browse the repository at this point in the history
fixed structure for comfort
  • Loading branch information
Apologiz authored Apr 10, 2019
2 parents 9d0236b + 67b124d commit ca47b60
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 73 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
steps:
- checkout
- run: |
go get -v -t -d ./...
go get -v -d ./...
go test -v -race -coverprofile=coverage.txt -covermode=atomic ./...
- run:
name: Build Success
Expand Down
51 changes: 27 additions & 24 deletions alog.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"runtime/debug"
"time"

"github.com/mylockerteam/alog/logger"
"github.com/mylockerteam/alog/strategy/file"
"github.com/mylockerteam/alog/strategy/standart"
)
Expand All @@ -28,7 +27,7 @@ const (

// Config contains settings and registered loggers
type Config struct {
Loggers logger.Map
Loggers Map
TimeFormat string
IgnoreFileLine bool
}
Expand Down Expand Up @@ -59,21 +58,21 @@ func Default(chanBuffer uint) Writer {
return &Log{config: config}
}

func getDefaultLoggerMap(chanBuffer uint) logger.Map {
return logger.Map{
logger.Info: &logger.Logger{
func getDefaultLoggerMap(chanBuffer uint) Map {
return Map{
Info: &Logger{
Channel: make(chan string, chanBuffer),
Strategies: []io.Writer{
&file.Strategy{File: os.Stdout},
},
},
logger.Wrn: &logger.Logger{
Wrn: &Logger{
Channel: make(chan string, chanBuffer),
Strategies: []io.Writer{
&file.Strategy{File: os.Stdout},
},
},
logger.Err: &logger.Logger{
Err: &Logger{
Channel: make(chan string, chanBuffer),
Strategies: []io.Writer{
&file.Strategy{File: os.Stderr},
Expand All @@ -84,10 +83,10 @@ func getDefaultLoggerMap(chanBuffer uint) logger.Map {

func printNotConfiguredMessage(code uint, skip int) {
if _, fileName, fileLine, ok := runtime.Caller(skip); ok {
log.Println(fmt.Sprintf("%s:%d Logger %s not configured", fileName, fileLine, logger.Name(code)))
log.Println(fmt.Sprintf("%s:%d Logger %s not configured", fileName, fileLine, Name(code)))
return
}
log.Println(fmt.Sprintf("Logger %s not configured", logger.Name(code)))
log.Println(fmt.Sprintf("Logger %s not configured", Name(code)))
}

// GetLoggerInterfaceByType returns io.Writer interface for logging in third-party libraries
Expand All @@ -101,55 +100,59 @@ func (a *Log) GetLoggerInterfaceByType(loggerType uint) io.Writer {

// Info method for recording informational messages
func (a *Log) Info(msg string) *Log {
if l := a.config.Loggers[logger.Info]; l != nil {
l.Channel <- a.prepareLog(time.Now(), msg, 2)
if l := a.config.Loggers[Info]; l != nil {
prepareLog := a.prepareLog(time.Now(), msg, 2)
l.Channel <- fmt.Sprintf("[%s] %s", Name(Info), prepareLog)
} else {
printNotConfiguredMessage(logger.Info, 2)
printNotConfiguredMessage(Info, 2)
}
return a
}

// Infof method of recording formatted informational messages
func (a *Log) Infof(format string, p ...interface{}) *Log {
if l := a.config.Loggers[logger.Info]; l != nil {
l.Channel <- a.prepareLog(time.Now(), fmt.Sprintf(format, p...), 2)
if l := a.config.Loggers[Info]; l != nil {
prepareLog := a.prepareLog(time.Now(), fmt.Sprintf(format, p...), 2)
l.Channel <- fmt.Sprintf("[%s] %s", Name(Info), prepareLog)
} else {
printNotConfiguredMessage(logger.Info, 2)
printNotConfiguredMessage(Info, 2)
}
return a
}

// Warning method for recording warning messages
func (a *Log) Warning(msg string) *Log {
if a.config.Loggers[logger.Wrn] != nil {
a.config.Loggers[logger.Wrn].Channel <- a.prepareLog(time.Now(), msg, 2)
if a.config.Loggers[Wrn] != nil {
prepareLog := a.prepareLog(time.Now(), msg, 2)
a.config.Loggers[Wrn].Channel <- fmt.Sprintf("[%s] %s", Name(Wrn), prepareLog)
} else {
printNotConfiguredMessage(logger.Wrn, 2)
printNotConfiguredMessage(Wrn, 2)
}
return a
}

// Method for recording errors without stack
func (a *Log) Error(err error) *Log {
if a.config.Loggers[logger.Err] != nil {
if a.config.Loggers[Err] != nil {
if err != nil {
a.config.Loggers[logger.Err].Channel <- a.prepareLog(time.Now(), err.Error(), 2)
prepareLog := a.prepareLog(time.Now(), err.Error(), 2)
a.config.Loggers[Err].Channel <- fmt.Sprintf("[%s] %s", Name(Err), prepareLog)
}
} else {
printNotConfiguredMessage(logger.Err, 2)
printNotConfiguredMessage(Err, 2)
}
return a
}

// ErrorDebug method for recording errors with stack
func (a *Log) ErrorDebug(err error) *Log {
if a.config.Loggers[logger.Err] != nil {
if a.config.Loggers[Err] != nil {
if err != nil {
msg := fmt.Sprintf(messageFormatErrorDebug, a.prepareLog(time.Now(), err.Error(), 2), string(debug.Stack()))
a.config.Loggers[logger.Err].Channel <- msg
a.config.Loggers[Err].Channel <- fmt.Sprintf("[%s] %s", Name(Err), msg)
}
} else {
printNotConfiguredMessage(logger.Err, 2)
printNotConfiguredMessage(Err, 2)
}
return a
}
Expand Down
15 changes: 7 additions & 8 deletions alog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@ import (
"testing"
"time"

"github.com/mylockerteam/alog/logger"
"github.com/mylockerteam/alog/strategy/file"
"github.com/mylockerteam/alog/strategy/standart"
"github.com/mylockerteam/alog/util"
)

const testMsg = "Hello, ALog!"

func loggerProvider() *logger.Logger {
return &logger.Logger{
func loggerProvider() *Logger {
return &Logger{
Channel: make(chan string, 1),
Strategies: []io.Writer{
file.Get(fmt.Sprintf("/tmp/%s/", util.RandString(10))),
Expand All @@ -34,8 +33,8 @@ func loggerProvider() *logger.Logger {

func configProvider() *Config {
return &Config{
Loggers: logger.Map{
logger.Info: loggerProvider(),
Loggers: Map{
Info: loggerProvider(),
},
}
}
Expand All @@ -61,9 +60,9 @@ func casesLogPrepareLog() []testsLogPrepareLog {
configSecond := configProvider()
loggerErr := loggerProvider()
loggerErr.Strategies = append(loggerErr.Strategies, file.Get(""))
configSecond.Loggers = logger.Map{
logger.Info: loggerProvider(),
logger.Err: loggerErr,
configSecond.Loggers = Map{
Info: loggerProvider(),
Err: loggerErr,
}
return []testsLogPrepareLog{
{
Expand Down
2 changes: 1 addition & 1 deletion logger/logger.go → logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Telegram: https://t.me/Apologiz
////////////////////////////////////////////////////////////////////////////////

package logger
package alog

import (
"errors"
Expand Down
17 changes: 1 addition & 16 deletions logger/logger_test.go → logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,15 @@
// Telegram: https://t.me/Apologiz
////////////////////////////////////////////////////////////////////////////////

package logger
package alog

import (
"fmt"
"io"
"testing"

"github.com/mylockerteam/alog/strategy/file"
"github.com/mylockerteam/alog/strategy/standart"
"github.com/mylockerteam/alog/util"
)

const testMsg = "Hello, ALog!"

func loggerProvider() *Logger {
return &Logger{
Channel: make(chan string, 1),
Strategies: []io.Writer{
file.Get(fmt.Sprintf("/tmp/%s/", util.RandString(10))),
standart.Get(),
},
}
}

type argsLoggerWriteMessage struct {
msg string
}
Expand Down
45 changes: 22 additions & 23 deletions writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"reflect"
"testing"

"github.com/mylockerteam/alog/logger"
"github.com/mylockerteam/alog/strategy/standart"
)

Expand All @@ -30,8 +29,8 @@ type testsLogInfo struct {
func casesLogInfo() []testsLogInfo {
info := configProvider()
wrn := &Config{
Loggers: logger.Map{
logger.Wrn: loggerProvider(),
Loggers: Map{
Wrn: loggerProvider(),
},
}
return []testsLogInfo{
Expand Down Expand Up @@ -80,8 +79,8 @@ type testsLogInfof struct {
func casesLogInfof() []testsLogInfof {
info := configProvider()
wrn := &Config{
Loggers: logger.Map{
logger.Wrn: loggerProvider(),
Loggers: Map{
Wrn: loggerProvider(),
},
}
return []testsLogInfof{
Expand Down Expand Up @@ -129,8 +128,8 @@ type testsLogWarning struct {
func casesLogWarning() []testsLogWarning {
info := configProvider()
wrn := &Config{
Loggers: logger.Map{
logger.Wrn: loggerProvider(),
Loggers: Map{
Wrn: loggerProvider(),
},
}
return []testsLogWarning{
Expand Down Expand Up @@ -178,13 +177,13 @@ func casesPrintNotConfiguredMessage() []testsPrintNotConfiguredMessage {
return []testsPrintNotConfiguredMessage{
{
args: argsPrintNotConfiguredMessage{
code: logger.Info,
code: Info,
skip: 2,
},
},
{
args: argsPrintNotConfiguredMessage{
code: logger.Info,
code: Info,
skip: 1000,
},
},
Expand Down Expand Up @@ -214,13 +213,13 @@ type testsLogGetLoggerInterfaceByType struct {
func casesLogGetLoggerInterfaceByType() []testsLogGetLoggerInterfaceByType {
config := configProvider()
wrn := &Config{
Loggers: logger.Map{
logger.Wrn: loggerProvider(),
Loggers: Map{
Wrn: loggerProvider(),
},
}
err := &Config{
Loggers: logger.Map{
logger.Err: loggerProvider(),
Loggers: Map{
Err: loggerProvider(),
},
}
return []testsLogGetLoggerInterfaceByType{
Expand All @@ -229,27 +228,27 @@ func casesLogGetLoggerInterfaceByType() []testsLogGetLoggerInterfaceByType {
config: config,
},
args: argsLogGetLoggerInterfaceByType{
loggerType: logger.Info,
loggerType: Info,
},
want: config.Loggers[logger.Info],
want: config.Loggers[Info],
},
{
fields: &Log{
config: wrn,
},
args: argsLogGetLoggerInterfaceByType{
loggerType: logger.Wrn,
loggerType: Wrn,
},
want: wrn.Loggers[logger.Wrn],
want: wrn.Loggers[Wrn],
},
{
fields: &Log{
config: err,
},
args: argsLogGetLoggerInterfaceByType{
loggerType: logger.Err,
loggerType: Err,
},
want: err.Loggers[logger.Err],
want: err.Loggers[Err],
},
{
fields: &Log{
Expand Down Expand Up @@ -288,8 +287,8 @@ type testsLogError struct {
func casesLogError() []testsLogError {
info := configProvider()
err := &Config{
Loggers: logger.Map{
logger.Err: loggerProvider(),
Loggers: Map{
Err: loggerProvider(),
},
}
return []testsLogError{
Expand Down Expand Up @@ -348,8 +347,8 @@ type testsLogErrorDebug struct {
func casesLogErrorDebug() []testsLogErrorDebug {
info := configProvider()
err := &Config{
Loggers: logger.Map{
logger.Err: loggerProvider(),
Loggers: Map{
Err: loggerProvider(),
},
}
return []testsLogErrorDebug{
Expand Down

0 comments on commit ca47b60

Please sign in to comment.