github.com/secoba/wails/v2@v2.6.4/internal/logger/default_logger.go (about)

     1  package logger
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/secoba/wails/v2/pkg/logger"
     8  )
     9  
    10  // LogLevel is an alias for the public LogLevel
    11  type LogLevel = logger.LogLevel
    12  
    13  // Logger is a utlility to log messages to a number of destinations
    14  type Logger struct {
    15  	output         logger.Logger
    16  	logLevel       LogLevel
    17  	showLevelInLog bool
    18  }
    19  
    20  // New creates a new Logger. You may pass in a number of `io.Writer`s that
    21  // are the targets for the logs
    22  func New(output logger.Logger) *Logger {
    23  	if output == nil {
    24  		output = logger.NewDefaultLogger()
    25  	}
    26  	result := &Logger{
    27  		logLevel:       logger.INFO,
    28  		showLevelInLog: true,
    29  		output:         output,
    30  	}
    31  
    32  	return result
    33  }
    34  
    35  // CustomLogger creates a new custom logger that prints out a name/id
    36  // before the messages
    37  func (l *Logger) CustomLogger(name string) CustomLogger {
    38  	return newcustomLogger(l, name)
    39  }
    40  
    41  // HideLogLevel removes the loglevel text from the start of each logged line
    42  func (l *Logger) HideLogLevel() {
    43  	l.showLevelInLog = true
    44  }
    45  
    46  // SetLogLevel sets the minimum level of logs that will be output
    47  func (l *Logger) SetLogLevel(level LogLevel) {
    48  	l.logLevel = level
    49  }
    50  
    51  // Writeln writes directly to the output with no log level
    52  // Appends a carriage return to the message
    53  func (l *Logger) Writeln(message string) {
    54  	l.output.Print(message)
    55  }
    56  
    57  // Write writes directly to the output with no log level
    58  func (l *Logger) Write(message string) {
    59  	l.output.Print(message)
    60  }
    61  
    62  // Print writes directly to the output with no log level
    63  // Appends a carriage return to the message
    64  func (l *Logger) Print(message string) {
    65  	l.Write(message)
    66  }
    67  
    68  // Trace level logging. Works like Sprintf.
    69  func (l *Logger) Trace(format string, args ...interface{}) {
    70  	if l.logLevel <= logger.TRACE {
    71  		l.output.Trace(fmt.Sprintf(format, args...))
    72  	}
    73  }
    74  
    75  // Debug level logging. Works like Sprintf.
    76  func (l *Logger) Debug(format string, args ...interface{}) {
    77  	if l.logLevel <= logger.DEBUG {
    78  		l.output.Debug(fmt.Sprintf(format, args...))
    79  	}
    80  }
    81  
    82  // Info level logging. Works like Sprintf.
    83  func (l *Logger) Info(format string, args ...interface{}) {
    84  	if l.logLevel <= logger.INFO {
    85  		l.output.Info(fmt.Sprintf(format, args...))
    86  	}
    87  }
    88  
    89  // Warning level logging. Works like Sprintf.
    90  func (l *Logger) Warning(format string, args ...interface{}) {
    91  	if l.logLevel <= logger.WARNING {
    92  		l.output.Warning(fmt.Sprintf(format, args...))
    93  	}
    94  }
    95  
    96  // Error level logging. Works like Sprintf.
    97  func (l *Logger) Error(format string, args ...interface{}) {
    98  	if l.logLevel <= logger.ERROR {
    99  		l.output.Error(fmt.Sprintf(format, args...))
   100  	}
   101  }
   102  
   103  // Fatal level logging. Works like Sprintf.
   104  func (l *Logger) Fatal(format string, args ...interface{}) {
   105  	l.output.Fatal(fmt.Sprintf(format, args...))
   106  	os.Exit(1)
   107  }