Refactoring logger

This commit is contained in:
Александр Федорюк 2022-12-01 11:12:55 +03:00
parent 8f9bd248e8
commit ea87f1a53a
7 changed files with 233 additions and 117 deletions

View File

@ -6,12 +6,13 @@
## Подготовка пакетов
```
```bash
apt install protobuf-compiler
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
```
## Подготовка файлов
```
protoc --go_out=. ./protobuf/*.proto --experimental_allow_proto3_optional
```bash
protoc --go_out=. ./protobuf/*.proto
```

8
go.mod
View File

@ -2,10 +2,4 @@ module git.ymnuktech.ru/ymnuk/yt-logger-go
go 1.19
require (
github.com/nats-io/nats.go v1.20.0 // indirect
github.com/nats-io/nkeys v0.3.0 // indirect
github.com/nats-io/nuid v1.0.1 // indirect
golang.org/x/crypto v0.0.0-20210314154223-e6e6c4f2bb5b // indirect
google.golang.org/protobuf v1.28.1 // indirect
)
require google.golang.org/protobuf v1.28.1

15
go.sum
View File

@ -1,18 +1,7 @@
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/nats-io/nats.go v1.20.0 h1:T8JJnQfVSdh1CzGiwAOv5hEobYCBho/0EupGznYw0oM=
github.com/nats-io/nats.go v1.20.0/go.mod h1:tLqubohF7t4z3du1QDPYJIQQyhb4wl6DhjxEajSI7UA=
github.com/nats-io/nkeys v0.3.0 h1:cgM5tL53EvYRU+2YLXIK0G2mJtK12Ft9oeooSZMA2G8=
github.com/nats-io/nkeys v0.3.0/go.mod h1:gvUNGjVcM2IPr5rCsRsC6Wb3Hr2CQAm08dsxtV6A5y4=
github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw=
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
golang.org/x/crypto v0.0.0-20210314154223-e6e6c4f2bb5b h1:wSOdpTq0/eI46Ez/LkDwIsAKA71YP2SRKBODiRWM0as=
golang.org/x/crypto v0.0.0-20210314154223-e6e6c4f2bb5b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=

View File

@ -26,7 +26,7 @@ type Time struct {
unknownFields protoimpl.UnknownFields
Time int64 `protobuf:"varint,1,opt,name=time,proto3" json:"time,omitempty"`
Nano int32 `protobuf:"varint,2,opt,name=nano,proto3" json:"nano,omitempty"`
Nano int64 `protobuf:"varint,2,opt,name=nano,proto3" json:"nano,omitempty"`
}
func (x *Time) Reset() {
@ -68,7 +68,7 @@ func (x *Time) GetTime() int64 {
return 0
}
func (x *Time) GetNano() int32 {
func (x *Time) GetNano() int64 {
if x != nil {
return x.Nano
}
@ -281,8 +281,9 @@ type Pkg struct {
TimeStart *Time `protobuf:"bytes,2,opt,name=timeStart,proto3" json:"timeStart,omitempty"`
TimeEnd *Time `protobuf:"bytes,3,opt,name=timeEnd,proto3" json:"timeEnd,omitempty"`
Metadata *Metadata `protobuf:"bytes,4,opt,name=metadata,proto3" json:"metadata,omitempty"`
Status *Status `protobuf:"bytes,5,opt,name=status,proto3" json:"status,omitempty"`
Err *Error `protobuf:"bytes,6,opt,name=err,proto3" json:"err,omitempty"`
Path string `protobuf:"bytes,5,opt,name=path,proto3" json:"path,omitempty"`
Status *Status `protobuf:"bytes,6,opt,name=status,proto3" json:"status,omitempty"`
Err *Error `protobuf:"bytes,7,opt,name=err,proto3" json:"err,omitempty"`
}
func (x *Pkg) Reset() {
@ -345,6 +346,13 @@ func (x *Pkg) GetMetadata() *Metadata {
return nil
}
func (x *Pkg) GetPath() string {
if x != nil {
return x.Path
}
return ""
}
func (x *Pkg) GetStatus() *Status {
if x != nil {
return x.Status
@ -366,7 +374,7 @@ var file_protobuf_log_pkg_proto_rawDesc = []byte{
0x6b, 0x67, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x03, 0x6c, 0x6f, 0x67, 0x22, 0x2e, 0x0a,
0x04, 0x54, 0x69, 0x6d, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x74, 0x69, 0x6d, 0x65, 0x18, 0x01, 0x20,
0x01, 0x28, 0x03, 0x52, 0x04, 0x74, 0x69, 0x6d, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6e,
0x6f, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x04, 0x6e, 0x61, 0x6e, 0x6f, 0x22, 0xa2, 0x01,
0x6f, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x04, 0x6e, 0x61, 0x6e, 0x6f, 0x22, 0xa2, 0x01,
0x0a, 0x08, 0x4d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0x12, 0x1a, 0x0a, 0x08, 0x66, 0x75,
0x6e, 0x63, 0x4e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x66, 0x75,
0x6e, 0x63, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x63, 0x61, 0x6c, 0x6c, 0x49, 0x44,
@ -385,7 +393,7 @@ var file_protobuf_log_pkg_proto_rawDesc = []byte{
0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x0a, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x43, 0x6f, 0x64,
0x65, 0x12, 0x12, 0x0a, 0x04, 0x73, 0x69, 0x7a, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52,
0x04, 0x73, 0x69, 0x7a, 0x65, 0x12, 0x10, 0x0a, 0x03, 0x6d, 0x73, 0x67, 0x18, 0x03, 0x20, 0x01,
0x28, 0x09, 0x52, 0x03, 0x6d, 0x73, 0x67, 0x22, 0xdd, 0x01, 0x0a, 0x03, 0x50, 0x6b, 0x67, 0x12,
0x28, 0x09, 0x52, 0x03, 0x6d, 0x73, 0x67, 0x22, 0xf1, 0x01, 0x0a, 0x03, 0x50, 0x6b, 0x67, 0x12,
0x1a, 0x0a, 0x08, 0x6c, 0x6f, 0x67, 0x4c, 0x65, 0x76, 0x65, 0x6c, 0x18, 0x01, 0x20, 0x01, 0x28,
0x05, 0x52, 0x08, 0x6c, 0x6f, 0x67, 0x4c, 0x65, 0x76, 0x65, 0x6c, 0x12, 0x27, 0x0a, 0x09, 0x74,
0x69, 0x6d, 0x65, 0x53, 0x74, 0x61, 0x72, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x09,
@ -395,12 +403,13 @@ var file_protobuf_log_pkg_proto_rawDesc = []byte{
0x52, 0x07, 0x74, 0x69, 0x6d, 0x65, 0x45, 0x6e, 0x64, 0x12, 0x29, 0x0a, 0x08, 0x6d, 0x65, 0x74,
0x61, 0x64, 0x61, 0x74, 0x61, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0d, 0x2e, 0x6c, 0x6f,
0x67, 0x2e, 0x4d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0x52, 0x08, 0x6d, 0x65, 0x74, 0x61,
0x64, 0x61, 0x74, 0x61, 0x12, 0x23, 0x0a, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x18, 0x05,
0x20, 0x01, 0x28, 0x0b, 0x32, 0x0b, 0x2e, 0x6c, 0x6f, 0x67, 0x2e, 0x53, 0x74, 0x61, 0x74, 0x75,
0x73, 0x52, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x12, 0x1c, 0x0a, 0x03, 0x65, 0x72, 0x72,
0x18, 0x06, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0a, 0x2e, 0x6c, 0x6f, 0x67, 0x2e, 0x45, 0x72, 0x72,
0x6f, 0x72, 0x52, 0x03, 0x65, 0x72, 0x72, 0x42, 0x07, 0x5a, 0x05, 0x2e, 0x2f, 0x6c, 0x6f, 0x67,
0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
0x64, 0x61, 0x74, 0x61, 0x12, 0x12, 0x0a, 0x04, 0x70, 0x61, 0x74, 0x68, 0x18, 0x05, 0x20, 0x01,
0x28, 0x09, 0x52, 0x04, 0x70, 0x61, 0x74, 0x68, 0x12, 0x23, 0x0a, 0x06, 0x73, 0x74, 0x61, 0x74,
0x75, 0x73, 0x18, 0x06, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0b, 0x2e, 0x6c, 0x6f, 0x67, 0x2e, 0x53,
0x74, 0x61, 0x74, 0x75, 0x73, 0x52, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x12, 0x1c, 0x0a,
0x03, 0x65, 0x72, 0x72, 0x18, 0x07, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0a, 0x2e, 0x6c, 0x6f, 0x67,
0x2e, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x52, 0x03, 0x65, 0x72, 0x72, 0x42, 0x07, 0x5a, 0x05, 0x2e,
0x2f, 0x6c, 0x6f, 0x67, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (

@ -1 +1 @@
Subproject commit 5c50b396ee7af804e45c9a9717eaf1c7cc242090
Subproject commit 28ac74d5f685ed6310f709ac4c60a47aeb73e92c

View File

@ -5,7 +5,6 @@ import (
"time"
"git.ymnuktech.ru/ymnuk/yt-logger-go/log"
"github.com/nats-io/nats.go"
)
type Priority int32
@ -37,81 +36,64 @@ const (
ColorMagentaBold = "\033[35;1m"
ColorRedBold = "\033[31;1m"
ColorYellowBold = "\033[33;1m"
ColorGreenBold = "\033[32;1m"
)
type YTLoggerWriter interface {
LogMode(Priority) YTLoggerWriter
Emerg(funcName, callID, callPrevID, callFrom, callResponseID *string, timeStart, timeEnd *time.Time, errCode *int32, err *string, statusCode, size *int64, msg *string)
Alert(funcName, callID, callPrevID, callFrom, callResponseID *string, timeStart, timeEnd *time.Time, errCode *int32, err *string, statusCode, size *int64, msg *string)
Crit(funcName, callID, callPrevID, callFrom, callResponseID *string, timeStart, timeEnd *time.Time, errCode *int32, err *string, statusCode, size *int64, msg *string)
Err(funcName, callID, callPrevID, callFrom, callResponseID *string, timeStart, timeEnd *time.Time, errCode *int32, err *string, statusCode, size *int64, msg *string)
Warning(funcName, callID, callPrevID, callFrom, callResponseID *string, timeStart, timeEnd *time.Time, errCode *int32, err *string, statusCode, size *int64, msg *string)
Notice(funcName, callID, callPrevID, callFrom, callResponseID *string, timeStart, timeEnd *time.Time, errCode *int32, err *string, statusCode, size *int64, msg *string)
Info(funcName, callID, callPrevID, callFrom, callResponseID *string, timeStart, timeEnd *time.Time, errCode *int32, err *string, statusCode, size *int64, msg *string)
Debug(funcName, callID, callPrevID, callFrom, callResponseID *string, timeStart, timeEnd *time.Time, errCode *int32, err *string, statusCode, size *int64, msg *string)
}
// Тип функции логирования в консоль
type LogF func(pkg *log.Pkg)
// Отправка логов в указанную очередь
type YTLoggerNats struct {
YTLoggerWriter
conn *nats.Conn
queue string
logLevel Priority
}
func NewYTLoggerNats(conn *nats.Conn, queue string, level Priority) (logger *YTLoggerNats) {
if conn == nil {
panic(fmt.Errorf("connection not set"))
}
if queue == "" {
panic(fmt.Errorf("queue name should be set"))
}
logger = &YTLoggerNats{
conn: conn,
queue: queue,
logLevel: level,
}
return
}
// LogMode log mode
func (l *YTLoggerNats) LogMode(level Priority) YTLoggerWriter {
newlogger := *l
newlogger.logLevel = level
return &newlogger
}
type YTLoggerConsole struct {
YTLoggerWriter
// Логер
type YTLogger struct {
logLevel Priority
streamMsg chan *log.Pkg
logF LogF
}
func NewYTLoggerConsole(level Priority) (logger *YTLoggerConsole) {
logger = &YTLoggerConsole{
// Создать логер
func NewYTLogger(level Priority, queueLen int, f LogF) (logger *YTLogger) {
if queueLen <= 0 {
queueLen = 1
}
logger = &YTLogger{
logLevel: level,
streamMsg: make(chan *log.Pkg, 1000000),
streamMsg: make(chan *log.Pkg, queueLen),
logF: f,
}
go logger.worker()
return
}
func (yt *YTLoggerConsole) Shutdown() {
close(yt.streamMsg)
}
// LogMode log mode
func (yt *YTLoggerConsole) LogMode(level Priority) *YTLoggerConsole {
newlogger := *yt
// Изменение уровня логирования
func (l *YTLogger) LogMode(level Priority) *YTLogger {
newlogger := *l
newlogger.logLevel = level
return &newlogger
}
func (yt *YTLoggerConsole) worker() {
// TODO
// Завершить логер
func (yt *YTLogger) Shutdown() {
close(yt.streamMsg)
}
func (yt *YTLoggerConsole) preparePkg(level Priority, funcName, callID, callPrevID, callFrom, callResponseID *string, timeStart, timeEnd *time.Time, errCode *int32, err *string, statusCode *int32, size *int64, msg *string) (pkg *log.Pkg) {
// Обработчик пакетов событий
func (yt *YTLogger) worker() {
for {
var pkg *log.Pkg
var ok bool
if pkg, ok = <-yt.streamMsg; !ok {
break
}
if pkg == nil {
continue
}
if yt.logF != nil {
yt.logF(pkg)
}
}
}
// Подготовка пакета
func (yt *YTLogger) preparePkg(level Priority, funcName, callID, callPrevID, callFrom, callResponseID *string, timeStart, timeEnd *time.Time, path *string, errCode *int32, err *string, statusCode *int32, size *int64, msg *string) (pkg *log.Pkg) {
if funcName == nil {
funcName = &[]string{""}[0]
}
@ -133,6 +115,11 @@ func (yt *YTLoggerConsole) preparePkg(level Priority, funcName, callID, callPrev
if timeEnd == nil {
timeEnd = &[]time.Time{time.Now()}[0]
}
if path == nil {
path = &[]string{""}[0]
}
if errCode == nil {
errCode = &[]int32{0}[0]
}
@ -154,11 +141,11 @@ func (yt *YTLoggerConsole) preparePkg(level Priority, funcName, callID, callPrev
LogLevel: int32(level),
TimeStart: &log.Time{
Time: timeStart.Unix(),
Nano: int32(timeStart.Nanosecond()),
Nano: int64(timeStart.Nanosecond()),
},
TimeEnd: &log.Time{
Time: timeEnd.Unix(),
Nano: int32(timeEnd.Nanosecond()),
Nano: int64(timeEnd.Nanosecond()),
},
Metadata: &log.Metadata{
FuncName: *funcName,
@ -167,59 +154,177 @@ func (yt *YTLoggerConsole) preparePkg(level Priority, funcName, callID, callPrev
CallFrom: *callFrom,
CallResponseID: *callResponseID,
},
Path: *path,
Status: &log.Status{
StatusCode: int32(*statusCode),
Size: *size,
Msg: *msg,
},
Err: &log.Error{
ErrCode: *errCode,
Err: *err,
},
}
return
}
func (yt *YTLoggerConsole) Emerg(funcName, callID, callPrevID, callFrom, callResponseID *string, timeStart, timeEnd *time.Time, errCode *int32, err *string, statusCode *int32, size *int64, msg *string) {
// Чрезвычайная ситуация
func (yt *YTLogger) Emerg(funcName, callID, callPrevID, callFrom, callResponseID *string, timeStart, timeEnd *time.Time, path *string, errCode *int32, err *string, statusCode *int32, size *int64, msg *string) {
if yt.logLevel >= LOG_EMERG {
yt.streamMsg <- yt.preparePkg(LOG_EMERG, funcName, callID, callPrevID, callFrom, callResponseID, timeStart, timeEnd, errCode, err, statusCode, size, msg)
yt.streamMsg <- yt.preparePkg(LOG_EMERG, funcName, callID, callPrevID, callFrom, callResponseID, timeStart, timeEnd, path, errCode, err, statusCode, size, msg)
}
}
func (yt *YTLoggerConsole) Alert(funcName, callID, callPrevID, callFrom, callResponseID *string, timeStart, timeEnd *time.Time, errCode *int32, err *string, statusCode *int32, size *int64, msg *string) {
// Тревожная ситуация
func (yt *YTLogger) Alert(funcName, callID, callPrevID, callFrom, callResponseID *string, timeStart, timeEnd *time.Time, path *string, errCode *int32, err *string, statusCode *int32, size *int64, msg *string) {
if yt.logLevel >= LOG_ALERT {
yt.streamMsg <- yt.preparePkg(LOG_ALERT, funcName, callID, callPrevID, callFrom, callResponseID, timeStart, timeEnd, errCode, err, statusCode, size, msg)
yt.streamMsg <- yt.preparePkg(LOG_ALERT, funcName, callID, callPrevID, callFrom, callResponseID, timeStart, timeEnd, path, errCode, err, statusCode, size, msg)
}
}
func (yt *YTLoggerConsole) Crit(funcName, callID, callPrevID, callFrom, callResponseID *string, timeStart, timeEnd *time.Time, errCode *int32, err *string, statusCode *int32, size *int64, msg *string) {
// Критическая ситуация
func (yt *YTLogger) Crit(funcName, callID, callPrevID, callFrom, callResponseID *string, timeStart, timeEnd *time.Time, path *string, errCode *int32, err *string, statusCode *int32, size *int64, msg *string) {
if yt.logLevel >= LOG_CRIT {
yt.streamMsg <- yt.preparePkg(LOG_CRIT, funcName, callID, callPrevID, callFrom, callResponseID, timeStart, timeEnd, errCode, err, statusCode, size, msg)
yt.streamMsg <- yt.preparePkg(LOG_CRIT, funcName, callID, callPrevID, callFrom, callResponseID, timeStart, timeEnd, path, errCode, err, statusCode, size, msg)
}
}
func (yt *YTLoggerConsole) Err(funcName, callID, callPrevID, callFrom, callResponseID *string, timeStart, timeEnd *time.Time, errCode *int32, err *string, statusCode *int32, size *int64, msg *string) {
// Ошибка
func (yt *YTLogger) Err(funcName, callID, callPrevID, callFrom, callResponseID *string, timeStart, timeEnd *time.Time, path *string, errCode *int32, err *string, statusCode *int32, size *int64, msg *string) {
if yt.logLevel >= LOG_ERR {
yt.streamMsg <- yt.preparePkg(LOG_ERR, funcName, callID, callPrevID, callFrom, callResponseID, timeStart, timeEnd, errCode, err, statusCode, size, msg)
yt.streamMsg <- yt.preparePkg(LOG_ERR, funcName, callID, callPrevID, callFrom, callResponseID, timeStart, timeEnd, path, errCode, err, statusCode, size, msg)
}
}
func (yt *YTLoggerConsole) Warning(funcName, callID, callPrevID, callFrom, callResponseID *string, timeStart, timeEnd *time.Time, errCode *int32, err *string, statusCode *int32, size *int64, msg *string) {
// Предупреждение
func (yt *YTLogger) Warning(funcName, callID, callPrevID, callFrom, callResponseID *string, timeStart, timeEnd *time.Time, path *string, errCode *int32, err *string, statusCode *int32, size *int64, msg *string) {
if yt.logLevel >= LOG_WARNING {
yt.streamMsg <- yt.preparePkg(LOG_WARNING, funcName, callID, callPrevID, callFrom, callResponseID, timeStart, timeEnd, errCode, err, statusCode, size, msg)
yt.streamMsg <- yt.preparePkg(LOG_WARNING, funcName, callID, callPrevID, callFrom, callResponseID, timeStart, timeEnd, path, errCode, err, statusCode, size, msg)
}
}
func (yt *YTLoggerConsole) Notice(funcName, callID, callPrevID, callFrom, callResponseID *string, timeStart, timeEnd *time.Time, errCode *int32, err *string, statusCode *int32, size *int64, msg *string) {
// Уведомление
func (yt *YTLogger) Notice(funcName, callID, callPrevID, callFrom, callResponseID *string, timeStart, timeEnd *time.Time, path *string, errCode *int32, err *string, statusCode *int32, size *int64, msg *string) {
if yt.logLevel >= LOG_NOTICE {
yt.streamMsg <- yt.preparePkg(LOG_NOTICE, funcName, callID, callPrevID, callFrom, callResponseID, timeStart, timeEnd, errCode, err, statusCode, size, msg)
yt.streamMsg <- yt.preparePkg(LOG_NOTICE, funcName, callID, callPrevID, callFrom, callResponseID, timeStart, timeEnd, path, errCode, err, statusCode, size, msg)
}
}
func (yt *YTLoggerConsole) Info(funcName, callID, callPrevID, callFrom, callResponseID *string, timeStart, timeEnd *time.Time, errCode *int32, err *string, statusCode *int32, size *int64, msg *string) {
// Информация
func (yt *YTLogger) Info(funcName, callID, callPrevID, callFrom, callResponseID *string, timeStart, timeEnd *time.Time, path *string, errCode *int32, err *string, statusCode *int32, size *int64, msg *string) {
if yt.logLevel >= LOG_INFO {
yt.streamMsg <- yt.preparePkg(LOG_INFO, funcName, callID, callPrevID, callFrom, callResponseID, timeStart, timeEnd, errCode, err, statusCode, size, msg)
yt.streamMsg <- yt.preparePkg(LOG_INFO, funcName, callID, callPrevID, callFrom, callResponseID, timeStart, timeEnd, path, errCode, err, statusCode, size, msg)
}
}
func (yt *YTLoggerConsole) Debug(funcName, callID, callPrevID, callFrom, callResponseID *string, timeStart, timeEnd *time.Time, errCode *int32, err *string, statusCode *int32, size *int64, msg *string) {
// Отладка
func (yt *YTLogger) Debug(funcName, callID, callPrevID, callFrom, callResponseID *string, timeStart, timeEnd *time.Time, path *string, errCode *int32, err *string, statusCode *int32, size *int64, msg *string) {
if yt.logLevel >= LOG_DEBUG {
yt.streamMsg <- yt.preparePkg(LOG_DEBUG, funcName, callID, callPrevID, callFrom, callResponseID, timeStart, timeEnd, errCode, err, statusCode, size, msg)
yt.streamMsg <- yt.preparePkg(LOG_DEBUG, funcName, callID, callPrevID, callFrom, callResponseID, timeStart, timeEnd, path, errCode, err, statusCode, size, msg)
}
}
// Логирования в консоль
func LogConsole(pkg *log.Pkg) {
if pkg == nil {
return
}
var color string
var level string
switch Priority(pkg.LogLevel) {
case LOG_EMERG:
level = "emerg"
color = ColorMagenta
case LOG_ALERT:
level = "alert"
color = ColorMagentaBold
case LOG_CRIT:
level = "crit"
color = ColorRedBold
case LOG_ERR:
level = "err"
color = ColorRed
case LOG_WARNING:
level = "warning"
color = ColorYellowBold
case LOG_NOTICE:
level = "notice"
color = ColorYellow
case LOG_INFO:
level = "info"
color = ColorGreenBold
case LOG_DEBUG:
level = "debug"
color = ColorGreen
default:
return
}
var timeStartStr string
if pkg.TimeStart != nil {
timeStartStr = time.Unix(pkg.TimeStart.Time, pkg.TimeStart.Nano).Format(time.RFC3339Nano)
} else {
timeStartStr = "-"
}
var timeEndStr string
if pkg.TimeEnd != nil {
timeEndStr = time.Unix(pkg.TimeStart.Time, pkg.TimeStart.Nano).Format(time.RFC3339Nano)
} else {
timeEndStr = "-"
}
if pkg.Path == "" {
pkg.Path = "-"
}
var msg string
if pkg.Status == nil {
msg = "-"
} else if pkg.Status.Msg == "" {
msg = "-"
} else {
msg = pkg.Status.Msg
}
if pkg.Path == "" {
pkg.Path = "-"
}
var funcName string
var callID string
var callFrom string
if pkg.Metadata == nil {
funcName = "-"
callID = "-"
callFrom = "-"
} else {
if pkg.Metadata.FuncName == "" {
funcName = "-"
} else {
funcName = pkg.Metadata.FuncName
}
if pkg.Metadata.CallID == "" {
callID = "-"
} else {
callID = pkg.Metadata.CallID
}
if pkg.Metadata.CallFrom == "" {
callFrom = "-"
} else {
callFrom = pkg.Metadata.CallFrom
}
}
fmt.Printf("%s[%s]%s %s %s %s %s %s %s %s%s",
color,
level,
ColorReset,
timeStartStr,
timeEndStr,
funcName,
callID,
callFrom,
pkg.Path,
msg,
"\n",
)
}

View File

@ -1,7 +1,6 @@
package ytlogger
import (
"fmt"
"testing"
"time"
)
@ -12,21 +11,40 @@ func TestMain(m *testing.M) {
shutdown()
}
var ytLoggerConsole *YTLoggerConsole
var ytLoggerNats *YTLoggerNats
var ytLoggerConsole *YTLogger
//var ytLoggerNats *YTLogger
func setup() {
ytLoggerConsole = NewYTLoggerConsole(LOG_DEBUG)
ytLoggerConsole = NewYTLogger(LOG_DEBUG, 100000, LogConsole)
}
func shutdown() {
ytLoggerConsole.Shutdown()
}
func TestPrintColors(t *testing.T) {
/*fmt.Println(ColorRed, " ColorRed")
fmt.Println(ColorGreen, " ColorGreen")
fmt.Println(ColorGreenBold, " ColorGreenBold")
fmt.Println(ColorYellow, " ColorYellow")
fmt.Println(ColorBlue, " ColorBlue")
fmt.Println(ColorMagenta, " ColorMagenta")
fmt.Println(ColorCyan, " ColorCyan")
fmt.Println(ColorWhite, " ColorWhite")
fmt.Println(ColorBlueBold, " ColorBlueBold")
fmt.Println(ColorMagentaBold, " ColorMagentaBold")
fmt.Println(ColorRedBold, " ColorRedBold")
fmt.Println(ColorYellowBold, " ColorYellowBold")
fmt.Println(ColorReset, " ColorReset")*/
}
func TestConsoleEmerg(t *testing.T) {
fmt.Println(time.Now().Unix())
/*fmt.Println(time.Now().Unix())
fmt.Println(time.Now().UnixMilli())
fmt.Println(time.Now().UnixMicro())
fmt.Println(time.Now().UnixNano())
fmt.Println(time.Now().Nanosecond())
fmt.Println(time.Now().Nanosecond())*/
ytLoggerConsole.Emerg(&[]string{"test1"}[0], &[]string{"test2"}[0], &[]string{"test3"}[0], &[]string{"test4"}[0], &[]string{"test5"}[0], &[]time.Time{time.Now()}[0], &[]time.Time{time.Now()}[0], &[]string{"test6"}[0], &[]int32{0}[0], nil, nil, &[]int64{0}[0], &[]string{"test"}[0])
<-time.After(time.Second * 29)
}