logger

package
v0.29.0-beta Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 30, 2024 License: MIT Imports: 8 Imported by: 0

README

Logger

Description

This tool is used to log messages to the console for a better debugging experience.

Features

  • Automatically expand objects and arrays to make it easier to read.
  • Provide a clear interface to log messages in colors.
  • While no name is provided, the tool will utilize the file name, the function name and the line of file as logger information.

Usage

  1. Import the package
     import 	"github.com/instill-ai/component/tools/logger"
    
  2. Create a new logger session
    logger := logger.SessionStart("Logger Name", VerboseLevel)
    defer logger.SessionEnd()
    
    • The first parameter is the name of the logger. If an empty name is provided, the logger will use the file name and the function name as session id.
    • The second parameter is the verbose level of the logger. The logger will only log messages with the level equal to or lower than the verbose level. The verbose level is an integer, and the logger provides the following levels:
      • Static : No message will be logged
      • Error : Only error messages will be logged
      • Warn : Error and warning messages will be logged
      • Develop: All messages will be logged
  3. Use the logger to log messages
    • Single message
    logger.Info("This is an info message")
    logger.Warn("This is a warning message")
    logger.Success("This is a success message")
    logger.Error("This is an error message")
    
    • Messages with a title (if the first parameter is type of string)
    logger.Info("Title 1", "This is an info message")
    logger.Info("Title 2", structA, structB, structC)
    // Output:
    // - Title 1: This is an info message
    // - Title 2: [
    //   structA {
    //     ...
    //   },
    //   structB {
    //     ...
    //   },
    //   ...
    // ]
    
    • Message with only non-string data
    logger.Info(structA, structB, structC)
    // Output:
    // - functionName:lineNumber : [
    //   structA {
    //     ...
    //   },
    //   structB {
    //     ...
    //   },
    //   ...
    // ]
    

Note

  • The default expand level is 5. If you want to expand more or less, you can change the value by calling the function logger.SetMaxDepth(level int).
  • The default indent symbol is " " (2 spaces) . You can change the indent symbol by calling the function logger.SetIndentSymbol(symbol string).

Documentation

Index

Constants

View Source
const (
	Static  = iota // Sessions set to this level will never print
	Error          // Sessions set to this level will only print errors
	Warn           // Sessions set to this level will print errors and warnings
	Develop        // Developing sessions will print all logs
)

Verbose Level for the logger to determine whether to print the log or not

Variables

This section is empty.

Functions

This section is empty.

Types

type Session

type Session struct {
	// contains filtered or unexported fields
}

func (*Session) DecrementIndent

func (d *Session) DecrementIndent() (self *Session)

func (*Session) Error

func (d *Session) Error(msg ...interface{})

Error logs messages with red color

func (*Session) IncrementIndent

func (d *Session) IncrementIndent() (self *Session)

func (*Session) Indent

func (d *Session) Indent() func() *Session

Increase the indentation level and return a function to decrease it

func (*Session) Info

func (d *Session) Info(msg ...interface{})

Info logs messages with black color

func (*Session) Raw

func (d *Session) Raw(msg ...interface{})

Log messages without expanding them

func (*Session) Separator

func (d *Session) Separator()

Separator adds a separator line

func (*Session) SessionEnd

func (d *Session) SessionEnd() (self *Session)

func (*Session) SessionStart

func (d *Session) SessionStart(name string, verboseLevel int) (self *Session)

Session Logger is only verbose when package verbose is greater than the verbose level specified here.

If no name is provided, the name will be <filename>:<function name> of the caller

func (*Session) SetIndentSymbol

func (d *Session) SetIndentSymbol(symbol string) (self *Session)

func (*Session) SetMaxDepth

func (d *Session) SetMaxDepth(depth int) (self *Session)

func (*Session) Success

func (d *Session) Success(msg ...interface{})

Success logs messages with green color

func (*Session) Warn

func (d *Session) Warn(msg ...interface{})

Warn logs messages with yellow color

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL