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

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