package
0.0.0-20230614145931-21a6d548466b
Repository: https://github.com/opensergo/opensergo-go.git
Documentation: pkg.go.dev

# README

logging component

here provider a common Logger interface which is highly extensible and multi implementable

Quick start

In this logging component, you can have only one global ConsoleLogger, and several FileLogger, By invoking function in logger.go:

    // AppendLoggerSlice add the Logger into loggerSlice
    func AppendLoggerSlice(loggerAppend Logger) {
        loggerSlice = append(loggerSlice, loggerAppend)
    }
    
    // SetConsoleLogger set the consoleLogger to print int stdout
    func SetConsoleLogger(logger Logger) {
        consoleLogger = logger
    }

You can use the default implement of Logger directly, By new Logger instance implemented in logger_default.go,
But first, you should new instances from logger_default.go, like following:

    // new instances
    logging.NewDefaultConsoleLogger(logging.DebugLevel)
    logging.NewDefaultFileLogger(logging.DebugLevel)
    // invoke the function in Logger interface
    logging.Error(errors.New("errors.New"), "this is error log in printErrorStack()")
    logging.Info("this is info log in printErrorStack()")
    logging.Debug("this is debug log in printErrorStack()")

You can format you log, by using logger_assembler.go,
the assembler provides AssembleMsgJsonFormat and AssembleMsgSeparateFormat can be chosen.

    // the unified entrance for AssembleMsg
    func AssembleMsg(logFormat LogFormat, callerDepth int, logLevel, msg string, err error, errWithStack bool, keysAndValues ...interface{}) string
    
    // AssembleMsgJsonFormat Assemble log-msg as json-format
    //
    // debug/info/warn:
    // {"logLevel":"INFO","timestamp":"2006-01-02 15:04:05.000","caller":"opensergo_client.go:74","msg":"openSergoClient is starting..."}
    //
    // error:
    // {"logLevel":"ERROR","timestamp":"2006-01-02 15:04:05.000","caller":"opensergo_client.go:83","msg":"can not connect.","error":"rpc error: code = Unavailable desc = connection error: desc = \"transport: Error while dialing dial tcp 33.1.33.1:10246: connect: connection refused\""}
    // [ ERROR CAUSES WITH STACK ][ rpc error: code = Unavailable desc = connection error: desc = "transport: Error while dialing dial tcp 33.1.33.1:10246: connect: connection refused"
    // github.com/opensergo/opensergo-go/pkg/client.handleReceive.func1
    //
    //	.../opensergo-go-sdk/pkg/client/opensergo_client.go:82 ][ ERROR CAUSES WITH STACK END ]
    func AssembleMsgJsonFormat(callerDepth int, logLevel, msg string, err error, errWithStack bool, keysAndValues ...interface{}) string
    
    // AssembleMsgSeparateFormat Assemble log-msg as separate-format
    //
    // pattern:
    // level | timestamp | callerFile:line | logContentJson | errorInfo
    //
    // debug/info/warn:
    // INFO | 2006-01-02 15:04:05.000 | main.go:30 | {"msg":"connected.", kvs:{}}
    //
    // error:
    // ERROR | 2006-01-02 15:04:05.000 | main.go:30 | {"msg":"connected.", kvs:{}} | rpc error: code = Unavailable desc = connection error: desc = "transport: Error while dialing dial tcp 33.1.33.1:10246: i/o timeout"
    // [ ERROR CAUSES WITH STACK ][ rpc error: code = Unavailable desc = connection error: desc = "transport: Error while dialing dial tcp 33.1.33.1:10246: connect: connection refused"
    // github.com/opensergo/opensergo-go/pkg/client.handleReceive.func1
    //
    //	.../opensergo-go-sdk/pkg/client/opensergo_client.go:82 ][ ERROR CAUSES WITH STACK END ]
    func AssembleMsgSeparateFormat(callerDepth int, logLevel, msg string, err error, errWithStack bool, keysAndValues ...interface{})

Samples

sample_simple_print

If you only want to use the Logger provided by default, you can new a ConsoleLogger, like following:

    // default ConsoleLogger
    // logging.NewDefaultConsoleLogger(logging.DebugLevel)
    // custom ConsoleLogger
    logging.NewConsoleLogger(logging.DebugLevel, logging.JsonFormat, true)
    logging.Error(errors.New("errors.New"), "this is error log in printErrorStack()")
    logging.Info("this is info log in printErrorStack()")
    logging.Debug("this is debug log in printErrorStack()")

For detail, please refer to sample_simple_print

sample_print_impl_logging

If you want to replace the logger implement where has integrated this logging component,
you can implement the Logger by your-self, append you Logger after invoke ClearLoggerSlice() in Logger, or invoke SetConsoleLogger(logger Logger) to set the global ConsoleLogger.

For detail, please refer to sample_print_impl_logging

sample_print_use_logging

If you want to use this logging component to replace the other has already integrated,
you can re-implement the other Logger by invoking the function in Logger which was instanced.

For detail, please refer to sample_print_use_logging

# Packages

No description provided by the author

# Functions

AddLogger add the Logger into loggerSlice.
AssembleMsg Assemble log-msg if logFormat is not matched, return JsonFormat as default.
AssembleMsgJsonFormat Assemble log-msg as json-format debug/info/warn: {"logLevel":"INFO","timestamp":"2006-01-02 15:04:05.000","caller":"opensergo_client.go:74","msg":"openSergoClient is starting..."} error: {"logLevel":"ERROR","timestamp":"2006-01-02 15:04:05.000","caller":"opensergo_client.go:83","msg":"can not connect.","error":"rpc error: code = Unavailable desc = connection error: desc = \"transport: Error while dialing dial tcp 33.1.33.1:10246: connect: connection refused\""} [ ERROR CAUSES WITH STACK ][ rpc error: code = Unavailable desc = connection error: desc = "transport: Error while dialing dial tcp 33.1.33.1:10246: connect: connection refused" github.com/opensergo/opensergo-go/pkg/client.handleReceive.func1 .../opensergo-go-sdk/pkg/client/opensergo_client.go:82 ][ ERROR CAUSES WITH STACK END ].
AssembleMsgSeparateFormat Assemble log-msg as separate-format pattern: level | timestamp | callerFile:line | logContentJson | errorInfo debug/info/warn: INFO | 2006-01-02 15:04:05.000 | main.go:30 | {"msg":"connected.", kvs:{}} error: ERROR | 2006-01-02 15:04:05.000 | main.go:30 | {"msg":"connected.", kvs:{}} | rpc error: code = Unavailable desc = connection error: desc = "transport: Error while dialing dial tcp 33.1.33.1:10246: i/o timeout" [ ERROR CAUSES WITH STACK ][ rpc error: code = Unavailable desc = connection error: desc = "transport: Error while dialing dial tcp 33.1.33.1:10246: connect: connection refused" github.com/opensergo/opensergo-go/pkg/client.handleReceive.func1 .../opensergo-go-sdk/pkg/client/opensergo_client.go:82 ][ ERROR CAUSES WITH STACK END ].
ClearLogger clear the loggerSlice.
Debug logs a non-error message with the given key/value pairs as context.
DebugWithCallerDepth logs a non-error message with the given key/value pairs as context.
Error logs an error message with error and the given key/value pairs as context.
ErrorWithCallerDepth logs an error message with error and the given key/value pairs as context.
Info logs a non-error message with the given key/value pairs as context.
InfoWithCallerDepth logs a non-error message with the given key/value pairs as context.
NewConsoleLogger new a ConsoleLogger to print logs in console.
NewDefaultConsoleLogger new a default ConsoleLogger to print logs in console.
NewDefaultFileLogger LoggerLevel DefaultLogLevel.
NewFileLogger new a Logger which write logs into file.
Print logs message no format as what the msg presents.
Warn logs a non-error message with the given key/value pairs as context.
WarnWithCallerDepth logs a non-error message with the given key/value pairs as context.

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Variables

No description provided by the author
No description provided by the author

# Structs

DefaultLogger default logger implements Logger.

# Interfaces

Logger the common interface for logging.

# Type aliases

Level represents the level of logging.
No description provided by the author