Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type ActionsLog ¶ added in v0.4.0
type ActionsLog int
ActionsLog is a log level in GitHub Actions.
const ( LogDebug ActionsLog = iota LogNotice LogWarn LogError )
func DefaultActionsLog ¶ added in v0.4.0
func DefaultActionsLog(level slog.Level) ActionsLog
DefaultActionsLog is the default mapping from slog.Level to ActionsLog.
func (ActionsLog) String ¶ added in v0.4.0
func (a ActionsLog) String() string
type Wrapper ¶ added in v0.3.0
type Wrapper struct {
// Handler is a function that returns the handler the Wrapper will wrap. Handler is only called once, so changes
// after the Wrapper is created will not be reflected. Defaults to DefaultHandler.
Handler func(w io.Writer) slog.Handler
// Output is the io.Writer that the Wrapper will write to. Defaults to os.Stdout because that is what GitHub
// Actions expects. Output should not be changed after the Wrapper is created.
Output io.Writer
// AddSource causes the Wrapper to compute the source code position
// of the log statement so that it can be linked from the GitHub Actions UI.
AddSource bool
// Level sets the level for the Wrapper itself. If it is set, the Wrapper will only pass through logs that
// are at or above this level. Handler may have its own level set as well. It is probably advisable
// to either set it on the Wrapper or the Handler but not both.
Level slog.Leveler
// ActionsLogger maps a slog.Level to an ActionsLog. Defaults to DefaultActionsLog. See ExampleWrapper_writeDebugToNotice for
// an example of a custom ActionsLogger.
ActionsLogger func(level slog.Level) ActionsLog
// contains filtered or unexported fields
}
Wrapper is a slog.Handler that wraps another slog.Handler and formats its output for GitHub Actions.
Example ¶
package main
import (
"fmt"
"golang.org/x/exp/slog"
"github.com/willabides/actionslog"
)
func main() {
logger := slog.New(&actionslog.Wrapper{})
logger = logger.With(slog.String("func", "Example"))
logger.Info("hello", slog.String("object", "world"))
logger.Warn("This is a stern warning")
logger.Error("got an error", slog.Any("err", fmt.Errorf("omg")))
logger.Debug("this is a debug message")
logger.Info("this is a \n multiline \r\n message")
logger.Info("multiline value", slog.String("value", "this is a\nmultiline\nvalue"))
}
Output: ::notice ::msg=hello func=Example object=world ::warning ::msg="This is a stern warning" func=Example ::error ::msg="got an error" func=Example err=omg ::debug ::msg="this is a debug message" func=Example ::notice ::msg="this is a \n multiline \r\n message" func=Example ::notice ::msg="multiline value" func=Example value="this is a\nmultiline\nvalue"
Example (WriteDebugToNotice) ¶
package main
import (
"golang.org/x/exp/slog"
"github.com/willabides/actionslog"
)
func main() {
// This gets around GitHub Actions' behavior of hiding debug messages unless you specify
// "enable debug logging" by outputting debug messages as notice messages.
logger := slog.New(&actionslog.Wrapper{
ActionsLogger: func(level slog.Level) actionslog.ActionsLog {
defaultLog := actionslog.DefaultActionsLog(level)
if defaultLog == actionslog.LogDebug {
return actionslog.LogNotice
}
return defaultLog
},
})
logger.Debug("this is a debug message")
logger.Info("this is an info message")
}
Output: ::notice ::msg="this is a debug message" ::notice ::msg="this is an info message"
Click to show internal directories.
Click to hide internal directories.


