github.com/AlpineAIO/wails/v2@v2.0.0-beta.32.0.20240505041856-1047a8fa5fef/internal/logger/custom_logger.go (about)

     1  package logger
     2  
     3  import (
     4  	"fmt"
     5  )
     6  
     7  // CustomLogger defines what a user can do with a logger
     8  type CustomLogger interface {
     9  	// Writeln writes directly to the output with no log level plus line ending
    10  	Writeln(message string)
    11  
    12  	// Write writes directly to the output with no log level
    13  	Write(message string)
    14  
    15  	// Trace level logging. Works like Sprintf.
    16  	Trace(format string, args ...interface{})
    17  
    18  	// Debug level logging. Works like Sprintf.
    19  	Debug(format string, args ...interface{})
    20  
    21  	// Info level logging. Works like Sprintf.
    22  	Info(format string, args ...interface{})
    23  
    24  	// Warning level logging. Works like Sprintf.
    25  	Warning(format string, args ...interface{})
    26  
    27  	// Error level logging. Works like Sprintf.
    28  	Error(format string, args ...interface{})
    29  
    30  	// Fatal level logging. Works like Sprintf.
    31  	Fatal(format string, args ...interface{})
    32  }
    33  
    34  // customLogger is a utlility to log messages to a number of destinations
    35  type customLogger struct {
    36  	logger *Logger
    37  	name   string
    38  }
    39  
    40  // New creates a new customLogger. You may pass in a number of `io.Writer`s that
    41  // are the targets for the logs
    42  func newcustomLogger(logger *Logger, name string) *customLogger {
    43  	result := &customLogger{
    44  		name:   name,
    45  		logger: logger,
    46  	}
    47  	return result
    48  }
    49  
    50  // Writeln writes directly to the output with no log level
    51  // Appends a carriage return to the message
    52  func (l *customLogger) Writeln(message string) {
    53  	l.logger.Writeln(message)
    54  }
    55  
    56  // Write writes directly to the output with no log level
    57  func (l *customLogger) Write(message string) {
    58  	l.logger.Write(message)
    59  }
    60  
    61  // Trace level logging. Works like Sprintf.
    62  func (l *customLogger) Trace(format string, args ...interface{}) {
    63  	format = fmt.Sprintf("%s | %s", l.name, format)
    64  	l.logger.Trace(format, args...)
    65  }
    66  
    67  // Debug level logging. Works like Sprintf.
    68  func (l *customLogger) Debug(format string, args ...interface{}) {
    69  	format = fmt.Sprintf("%s | %s", l.name, format)
    70  	l.logger.Debug(format, args...)
    71  }
    72  
    73  // Info level logging. Works like Sprintf.
    74  func (l *customLogger) Info(format string, args ...interface{}) {
    75  	format = fmt.Sprintf("%s | %s", l.name, format)
    76  	l.logger.Info(format, args...)
    77  }
    78  
    79  // Warning level logging. Works like Sprintf.
    80  func (l *customLogger) Warning(format string, args ...interface{}) {
    81  	format = fmt.Sprintf("%s | %s", l.name, format)
    82  	l.logger.Warning(format, args...)
    83  }
    84  
    85  // Error level logging. Works like Sprintf.
    86  func (l *customLogger) Error(format string, args ...interface{}) {
    87  	format = fmt.Sprintf("%s | %s", l.name, format)
    88  	l.logger.Error(format, args...)
    89  }
    90  
    91  // Fatal level logging. Works like Sprintf.
    92  func (l *customLogger) Fatal(format string, args ...interface{}) {
    93  	format = fmt.Sprintf("%s | %s", l.name, format)
    94  	l.logger.Fatal(format, args...)
    95  }