跳至主要内容
版本: v2.9.0

日志

Wails 运行时提供了一个日志记录机制,可以从 Go 或 JavaScript 中调用。与大多数记录器一样,它有许多日志级别

  • 跟踪
  • 调试
  • 信息
  • 警告
  • 错误
  • 致命

记录器将在当前或更高的日志级别输出任何日志消息。例如:Debug 日志级别将输出除 Trace 消息之外的所有消息。

LogPrint

将给定的消息作为原始消息记录。

Go: LogPrint(ctx context.Context, message string)
JS: LogPrint(message: string)

LogPrintf

将给定的消息作为原始消息记录。

Go: LogPrintf(ctx context.Context, format string, args ...interface{})

LogTrace

Trace 日志级别记录给定的消息。

Go: LogTrace(ctx context.Context, message string)
JS: LogTrace(message: string)

LogTracef

Trace 日志级别记录给定的消息。

Go: LogTracef(ctx context.Context, format string, args ...interface{})

LogDebug

Debug 日志级别记录给定的消息。

Go: LogDebug(ctx context.Context, message string)
JS: LogDebug(message: string)

LogDebugf

Debug 日志级别记录给定的消息。

Go: LogDebugf(ctx context.Context, format string, args ...interface{})

LogInfo

Info 日志级别记录给定的消息。

Go: LogInfo(ctx context.Context, message string)
JS: LogInfo(message: string)

LogInfof

Info 日志级别记录给定的消息。

Go: LogInfof(ctx context.Context, format string, args ...interface{})

LogWarning

Warning 日志级别记录给定的消息。

Go: LogWarning(ctx context.Context, message string)
JS: LogWarning(message: string)

LogWarningf

Warning 日志级别记录给定的消息。

Go: LogWarningf(ctx context.Context, format string, args ...interface{})

LogError

Error 日志级别记录给定的消息。

Go: LogError(ctx context.Context, message string)
JS: LogError(message: string)

LogErrorf

Error 日志级别记录给定的消息。

Go: LogErrorf(ctx context.Context, format string, args ...interface{})

LogFatal

Fatal 日志级别记录给定的消息。

Go: LogFatal(ctx context.Context, message string)
JS: LogFatal(message: string)

LogFatalf

Fatal 日志级别记录给定的消息。

Go: LogFatalf(ctx context.Context, format string, args ...interface{})

LogSetLogLevel

设置日志级别。在 JavaScript 中,数字与以下日志级别相关

日志级别
1跟踪
2调试
3信息
4警告
5错误

Go: LogSetLogLevel(ctx context.Context, level logger.LogLevel)
JS: LogSetLogLevel(level: number)

使用自定义记录器

可以通过使用 Logger 应用程序选项来提供自定义记录器。唯一的要求是记录器实现 github.com/wailsapp/wails/v2/pkg/logger 中定义的 logger.Logger 接口

logger.go
type Logger interface {
Print(message string)
Trace(message string)
Debug(message string)
Info(message string)
Warning(message string)
Error(message string)
Fatal(message string)
}