* move logger under platform
Этот коммит содержится в:
Ibrahim Serdar Acikgoz
2022-08-24 10:10:56 +03:00
коммит произвёл GitHub
родитель 6a6e05073e
Коммит efbcb0a35a
36 изменённых файлов: 327 добавлений и 297 удалений

Просмотреть файл

@@ -21,7 +21,6 @@ import (
type ServiceConfig struct {
// Mandatory fields
ConfigStore *config.Store
Logger *mlog.Logger
StartMetrics bool // TODO: find an elegant way to start/stop metrics server by default
// Optional fields
Metrics einterfaces.MetricsInterface
@@ -34,16 +33,6 @@ func (c *ServiceConfig) validate() error {
return errors.New("ConfigStore is required")
}
if c.Logger == nil {
var err error
// If Logger is not set, use a default logger temporarily.
// this should be removed once the logger is properly configured with the service config.
// MM-45841
c.Logger, err = mlog.NewLogger()
if err != nil {
return err
}
}
return nil
}

117
app/platform/log.go Обычный файл
Просмотреть файл

@@ -0,0 +1,117 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package platform
import (
"context"
"errors"
"fmt"
"time"
"github.com/mattermost/mattermost-server/v6/config"
"github.com/mattermost/mattermost-server/v6/model"
"github.com/mattermost/mattermost-server/v6/shared/mlog"
)
func (ps *PlatformService) ReconfigureLogger() error {
return ps.initLogging()
}
// initLogging initializes and configures the logger(s). This may be called more than once.
func (ps *PlatformService) initLogging() error {
// create the app logger if needed
if ps.logger == nil {
var err error
ps.logger, err = mlog.NewLogger()
if err != nil {
return err
}
logCfg, err := config.MloggerConfigFromLoggerConfig(&ps.Config().LogSettings, nil, config.GetLogFileLocation)
if err != nil {
return err
}
if errCfg := ps.logger.ConfigureTargets(logCfg, nil); errCfg != nil {
return fmt.Errorf("failed to configure test logger: %w", errCfg)
}
}
// create notification logger if needed
if ps.notificationsLogger == nil {
l, err := mlog.NewLogger()
if err != nil {
return err
}
ps.notificationsLogger = l.With(mlog.String("logSource", "notifications"))
}
if err := ps.ConfigureLogger("logging", ps.logger, &ps.Config().LogSettings, config.GetLogFileLocation); err != nil {
// if the config is locked then a unit test has already configured and locked the logger; not an error.
if !errors.Is(err, mlog.ErrConfigurationLock) {
// revert to default logger if the config is invalid
mlog.InitGlobalLogger(nil)
return err
}
}
// Redirect default Go logger to app logger.
ps.logger.RedirectStdLog(mlog.LvlStdLog)
// Use the app logger as the global logger (eventually remove all instances of global logging).
mlog.InitGlobalLogger(ps.logger)
notificationLogSettings := config.GetLogSettingsFromNotificationsLogSettings(&ps.Config().NotificationLogSettings)
if err := ps.ConfigureLogger("notification logging", ps.notificationsLogger, notificationLogSettings, config.GetNotificationsLogFileLocation); err != nil {
if !errors.Is(err, mlog.ErrConfigurationLock) {
mlog.Error("Error configuring notification logger", mlog.Err(err))
return err
}
}
return nil
}
func (ps *PlatformService) Logger() *mlog.Logger {
return ps.logger
}
func (ps *PlatformService) NotificationsLogger() *mlog.Logger {
return ps.notificationsLogger
}
func (ps *PlatformService) EnableLoggingMetrics() {
if ps.metrics == nil || ps.metrics.metricsImpl == nil {
return
}
ps.logger.SetMetricsCollector(ps.metrics.metricsImpl.GetLoggerMetricsCollector(), mlog.DefaultMetricsUpdateFreqMillis)
// logging config needs to be reloaded when metrics collector is added or changed.
if err := ps.initLogging(); err != nil {
mlog.Error("Error re-configuring logging for metrics")
return
}
mlog.Debug("Logging metrics enabled")
}
// RemoveUnlicensedLogTargets removes any unlicensed log target types.
func (ps *PlatformService) RemoveUnlicensedLogTargets(license *model.License) {
if license != nil && *license.Features.AdvancedLogging {
// advanced logging enabled via license; no need to remove any targets
return
}
timeoutCtx, cancelCtx := context.WithTimeout(context.Background(), time.Second*10)
defer cancelCtx()
ps.logger.RemoveTargets(timeoutCtx, func(ti mlog.TargetInfo) bool {
return ti.Type != "*targets.Writer" && ti.Type != "*targets.File"
})
ps.notificationsLogger.RemoveTargets(timeoutCtx, func(ti mlog.TargetInfo) bool {
return ti.Type != "*targets.Writer" && ti.Type != "*targets.File"
})
}

Просмотреть файл

@@ -20,7 +20,9 @@ import (
type PlatformService struct {
serviceConfig ServiceConfig
configStore *config.Store
logger *mlog.Logger
logger *mlog.Logger
notificationsLogger *mlog.Logger
metrics *platformMetrics
@@ -44,10 +46,13 @@ func New(sc ServiceConfig) (*PlatformService, error) {
ps := &PlatformService{
serviceConfig: sc,
configStore: sc.ConfigStore,
logger: sc.Logger,
cluster: sc.Cluster,
}
if err := ps.initLogging(); err != nil {
return nil, fmt.Errorf("failed to initialize logging: %w", err)
}
if err := ps.resetMetrics(sc.Metrics, ps.configStore.Get); err != nil {
return nil, err
}
@@ -77,3 +82,7 @@ func (ps *PlatformService) ShutdownConfig() error {
func (ps *PlatformService) SetTelemetryId(id string) {
ps.telemetryId = id
}
func (ps *PlatformService) SetLogger(logger *mlog.Logger) {
ps.logger = logger
}