gcron supports logging functionality, allowing the setting of log output files and levels. By default, it only outputs logs at the LEVEL_WARN | LEVEL_ERRO | LEVEL_CRIT error levels (including scheduled task exceptions), with runtime logs recorded at the LEVEL_DEBUG level, which are not recorded by default. The gcron component uses the unified logging component of the goframe framework, allowing reuse of all the logging component features. Relevant methods:

    1. func SetLogger(logger glog.ILogger)
    2. func GetLogger() glog.ILogger

    Cron Job - Logging - 图1tip

    For logging component features, please refer to the Logging section.

    Usage example:

    1. package main
    2. import (
    3. "context"
    4. "github.com/gogf/gf/v2/frame/g"
    5. "github.com/gogf/gf/v2/os/gcron"
    6. "github.com/gogf/gf/v2/os/gctx"
    7. "github.com/gogf/gf/v2/os/glog"
    8. "time"
    9. )
    10. func main() {
    11. var (
    12. err error
    13. ctx = gctx.New()
    14. logger = glog.New()
    15. )
    16. logger.SetLevel(glog.LEVEL_ALL)
    17. gcron.SetLogger(logger)
    18. _, err = gcron.Add(ctx, "* * * * * ?", func(ctx context.Context) {
    19. g.Log().Info(ctx, "test")
    20. })
    21. if err != nil {
    22. panic(err)
    23. }
    24. time.Sleep(3 * time.Second)
    25. }

    After execution, the terminal output is:

    Cron Job - Logging - 图2