Skip to content

Commit

Permalink
v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
DrOctavius committed Oct 3, 2024
0 parents commit 177177d
Show file tree
Hide file tree
Showing 17 changed files with 1,437 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/dist
tmp/**

# Editor directories and files
.idea
.vscode
.history
1 change: 1 addition & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1. Remove colors from file logs, they should be filtered before writing to file!
63 changes: 63 additions & 0 deletions appLog/logs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package appLog

import (
"github.com/kyaxcorp/go-logger"
"github.com/kyaxcorp/go-logger/model"
"github.com/rs/zerolog"
)

func getApp() *model.Logger {
return logger.GetAppLogger()
}

//--------------------------\\

func Info() *zerolog.Event {
return getApp().Info()
}

func Warn() *zerolog.Event {
return getApp().Warn()
}

func Error() *zerolog.Event {
return getApp().Error()
}

func Debug() *zerolog.Event {
return getApp().Debug()
}

func Fatal() *zerolog.Event {
return getApp().Fatal()
}

func Panic() *zerolog.Event {
return getApp().Panic()
}

//--------------------------\\

func InfoF(functionName string) *zerolog.Event {
return getApp().InfoF(functionName)
}

func WarnF(functionName string) *zerolog.Event {
return getApp().WarnF(functionName)
}

func ErrorF(functionName string) *zerolog.Event {
return getApp().ErrorF(functionName)
}

func DebugF(functionName string) *zerolog.Event {
return getApp().DebugF(functionName)
}

func FatalF(functionName string) *zerolog.Event {
return getApp().FatalF(functionName)
}

func PanicF(functionName string) *zerolog.Event {
return getApp().PanicF(functionName)
}
67 changes: 67 additions & 0 deletions application/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package application

import (
"os"

configEvents "github.com/kyaxcorp/go-core/core/config/events"
"github.com/kyaxcorp/go-helper/conv"
"github.com/kyaxcorp/go-logger"
"github.com/kyaxcorp/go-logger/application/vars"
loggerConfig "github.com/kyaxcorp/go-logger/config"
loggerPaths "github.com/kyaxcorp/go-logger/paths"
)

// Define variables
var applicationLoggerConfig loggerConfig.Config
var coreLoggerConfig loggerConfig.Config

type MainLogOptions struct {
Level int
}

func CreateAppLogger(o MainLogOptions) {
applicationLoggerConfig, _ = loggerConfig.DefaultConfig(&loggerConfig.Config{
IsEnabled: "yes",
Name: "application",
ModuleName: "Application",
Description: "saving all application logs...",
Level: o.Level,
DirLogPath: loggerPaths.GetApplicationLogsPath(),
// We set to yes, because this is the main Application Logger from which others will extend
IsApplication: "yes",
})
// This is the Application Logger, it will save all logs
vars.ApplicationLogger = logger.New(applicationLoggerConfig)
}

func RegisterAppLogger() {
var _, _ = configEvents.OnLoaded(func() {
CreateAppLogger(MainLogOptions{})
})
}

func CreateCoreLogger() bool {
logLevel := os.Getenv("GO_CORE_LOG_LEVEL")
var lvl int
if logLevel == "" {
lvl = 4
} else {
lvl = conv.StrToInt(logLevel)
}

coreLoggerConfig, _ = loggerConfig.DefaultConfig(&loggerConfig.Config{
IsEnabled: "yes",
Name: "core",
ModuleName: "Core",
Description: "saving all core logs...",
Level: lvl, // take from the environment

FileIsEnabled: "no",
ConsoleIsEnabled: "yes",
})
// This is the Application Logger, it will save all logs
vars.CoreLogger = logger.New(coreLoggerConfig)
return true
}

var _ = CreateCoreLogger()
10 changes: 10 additions & 0 deletions application/vars/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package vars

import "github.com/kyaxcorp/go-logger/model"

// ApplicationLogger -> This is the app logger which handles all logs writing to a single file
var ApplicationLogger *model.Logger

// CoreLogger -> this is the first logger which is been created...
// it's more for debugging lib things
var CoreLogger *model.Logger
76 changes: 76 additions & 0 deletions channel/channel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package channel

import (
mainConfig "github.com/kyaxcorp/go-core/core/config"
"github.com/kyaxcorp/go-helper/errors2"
"github.com/kyaxcorp/go-logger"
"github.com/kyaxcorp/go-logger/model"
loggerPaths "github.com/kyaxcorp/go-logger/paths"
"github.com/rs/zerolog"
)

type Config struct {
ChannelName string
ReturnDefaultIfNotExists bool
}

// GetDefaultChannel -> gets the default logger based on the current configuration
// Finds the default one
// checks if the object is created in memory
// if yes then returns it, if not it creates it based on the configuration
func GetDefaultChannel() (*model.Logger, error) {
cfg := mainConfig.GetConfig()
if cfg.Logging.DefaultChannel == "" {
msg := "logger default channel name is empty"
l().Warn().Msg(msg)
return nil, errors2.New(0, msg)
}
return GetChannel(Config{
ChannelName: cfg.Logging.DefaultChannel,
})
}

// GetChannel -> Get channel based on instance name (the key from the config)
// Check if there is a channel like this...
// If there is not, then return an error...
// If there is, return the logger based on the config found
func GetChannel(c Config) (*model.Logger, error) {
cfg := mainConfig.GetConfig()
// Check if exists
if _, ok := cfg.Logging.Channels[c.ChannelName]; !ok {
// Doesn't exist
if c.ReturnDefaultIfNotExists {
// Return the default one!
return GetDefaultChannel()
}

msg := "logger channel doesn't exist"
l().Warn().Str("logger_channel", c.ChannelName).Msg(msg)
return nil, errors2.New(0, msg)
}

// Exists
instanceConfig := cfg.Logging.Channels[c.ChannelName]

// Setting default values if some of them are missing in the config!
// Setting instance name by key
if instanceConfig.Name == "" {
instanceConfig.Name = c.ChannelName
}
// If DirLogPath is not defined, it will set the default folder!
if instanceConfig.DirLogPath == "" {
instanceConfig.DirLogPath = loggerPaths.GetLogsPathForChannels("websocket/" + instanceConfig.Name)
}

// Set Module Name
if instanceConfig.ModuleName == "" {
instanceConfig.ModuleName = instanceConfig.Name
}

return logger.New(instanceConfig), nil
}

// log -> This is for local use only
func l() *zerolog.Logger {
return logger.GetAppLogger().Logger
}
90 changes: 90 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package config

import (
"io"

"github.com/kyaxcorp/go-helper/_struct"
"github.com/rs/zerolog"
)

type Logger interface {
GetLogger() *zerolog.Logger
}

type Config struct {
// IsEnabled -> enable/disable the logging (this is generally)
// By default will be enabled, and the default level will be 4 -> which is Warn!
IsEnabled string `yaml:"is_enabled" mapstructure:"is_enabled" default:"yes"`
// Name -> this is the identifier of the instance
Name string
// ModuleName -> identifies the module in logs...Each component should set its own module name
ModuleName string `yaml:"module_name" mapstructure:"module_name" default:""`

// Description -> something about it
Description string

EncodeLogsAsJson string `yaml:"encode_logs_as_json" mapstructure:"encode_logs_as_json" default:"yes"`

// CliIsEnabled -> If console logging is enabled
ConsoleIsEnabled string `yaml:"console_is_enabled" mapstructure:"console_is_enabled" default:"yes"`

ConsoleTimeFormat string `yaml:"console_time_format" mapstructure:"console_time_format" default:"002 15:04:05.000"`

// Level -> the level which should be for the output, it's for Console and for the file log!
// For more specific levels:
/*
1: zerolog.TraceLevel,
2: zerolog.DebugLevel,
3: zerolog.InfoLevel,
4: zerolog.WarnLevel,
5: zerolog.ErrorLevel,
6: zerolog.FatalLevel,
7: zerolog.PanicLevel,
*/
// The default one is INFO!
// We will not set the default value... so it will be by default 0 -> meaning DEBUG, we don't set, because when it's 0
// it's counted as initial value!
// We will set the default value to 2 -> Debug Level
Level int `yaml:"level" mapstructure:"level" default:"4"`

// FileName -> is optional, and can be set only if the user wants to override the default name, or to override the full
// path of the writing file... it can be user with DirLogPath
FileName string `yaml:"file_name" mapstructure:"file_name" default:"-"`

// FileIsEnabled -> if file logging is enabled
FileIsEnabled string `yaml:"file_is_enabled" mapstructure:"file_is_enabled" default:"yes"`
// LogPath -> where the logs should be saved
DirLogPath string `yaml:"dir_log_path" mapstructure:"dir_log_path" default:""`
// RotateMaxFiles -> how many files should be in the same folder, it will auto delete the old ones
FileRotateMaxFiles int `yaml:"file_rotate_max_files" mapstructure:"file_rotate_max_files" default:"50"`
// LogMaxSize -> the maximum size of a log file, if it's getting big, it will be created a new one -> default 5 MB
FileLogMaxSize int `yaml:"file_log_max_size_mb" mapstructure:"file_log_max_size_mb" default:"5"`
// MaxAge the max age in days to keep a logfile
FileMaxAge int `yaml:"file_max_age_days" mapstructure:"file_max_age_days" default:"30"`
// Is the main logger from which everyone will extend!? We don't need to export it!
IsApplication string `yaml:"-" mapstructure:"-" default:"-"`

// This is the parent... so the child can write to parent log
//ParentLogger Config
ParentWriter io.Writer `yaml:"-" mapstructure:"-" default:"-"`
// WriteToParent -> also write to parent log file
WriteToParent string `yaml:"write_to_parent" mapstructure:"write_to_parent" default:"yes"`

// This is an interface to get to logger object directly
Logger Logger `yaml:"-" mapstructure:"-" default:"-"`
}

// DefaultConfig -> it will return the default config with default values
func DefaultConfig(configObj ...*Config) (Config, error) {
var c *Config
if len(configObj) > 0 {
c = configObj[0]
}

if c == nil {
c = &Config{}
}
// Set the default values for the object!
_err := _struct.SetDefaultValues(c)
return *c, _err
}
Loading

0 comments on commit 177177d

Please sign in to comment.