github.com/goravel/framework@v1.13.9/log/application.go (about)

     1  package log
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/gookit/color"
     7  	"github.com/sirupsen/logrus"
     8  
     9  	"github.com/goravel/framework/contracts/config"
    10  	"github.com/goravel/framework/contracts/log"
    11  )
    12  
    13  type Application struct {
    14  	log.Writer
    15  	instance *logrus.Logger
    16  }
    17  
    18  func NewApplication(config config.Config) *Application {
    19  	instance := logrus.New()
    20  	instance.SetLevel(logrus.DebugLevel)
    21  
    22  	if config != nil {
    23  		if logging := config.GetString("logging.default"); logging != "" {
    24  			if err := registerHook(config, instance, logging); err != nil {
    25  				color.Redln("Init facades.Log error: " + err.Error())
    26  
    27  				return nil
    28  			}
    29  		}
    30  	}
    31  
    32  	return &Application{
    33  		instance: instance,
    34  		Writer:   NewWriter(instance.WithContext(context.Background())),
    35  	}
    36  }
    37  
    38  func (r *Application) WithContext(ctx context.Context) log.Writer {
    39  	switch r.Writer.(type) {
    40  	case *Writer:
    41  		return NewWriter(r.instance.WithContext(ctx))
    42  	default:
    43  		return r.Writer
    44  	}
    45  }