3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-14 16:09:32 +01:00

logger: Very initial working new logs

This commit is contained in:
Daniel Oaks 2017-03-06 15:50:23 +10:00
parent 45dd882ec4
commit 29e89b16a0
5 changed files with 51 additions and 33 deletions

View File

@ -402,7 +402,7 @@ func (client *Client) AllNickmasks() []string {
// SetNickname sets the very first nickname for the client. // SetNickname sets the very first nickname for the client.
func (client *Client) SetNickname(nickname string) error { func (client *Client) SetNickname(nickname string) error {
if client.HasNick() { if client.HasNick() {
Log.error.Printf("%s nickname already set!", client.nickMaskString) client.server.logger.Log(LogError, "nick", client.nick, fmt.Sprintf("%s nickname already set, something is wrong with server consistency", client.nickMaskString))
return ErrNickAlreadySet return ErrNickAlreadySet
} }

View File

@ -355,8 +355,10 @@ func LoadConfig(filename string) (config *Config, err error) {
if config.Limits.LineLen.Tags < 512 || config.Limits.LineLen.Rest < 512 { if config.Limits.LineLen.Tags < 512 || config.Limits.LineLen.Rest < 512 {
return nil, errors.New("Line lengths must be 512 or greater (check the linelen section under server->limits)") return nil, errors.New("Line lengths must be 512 or greater (check the linelen section under server->limits)")
} }
var newLogConfigs []LoggingConfig
for _, logConfig := range config.Logging { for _, logConfig := range config.Logging {
// methods // methods
logConfig.Methods = make(map[string]bool)
for _, method := range strings.Split(logConfig.Method, " ") { for _, method := range strings.Split(logConfig.Method, " ") {
if len(method) > 0 { if len(method) > 0 {
logConfig.Methods[strings.ToLower(method)] = true logConfig.Methods[strings.ToLower(method)] = true
@ -374,6 +376,8 @@ func LoadConfig(filename string) (config *Config, err error) {
logConfig.Level = level logConfig.Level = level
// types // types
logConfig.Types = make(map[string]bool)
logConfig.ExcludedTypes = make(map[string]bool)
for _, typeStr := range strings.Split(logConfig.TypeString, " ") { for _, typeStr := range strings.Split(logConfig.TypeString, " ") {
if len(typeStr) == 0 { if len(typeStr) == 0 {
continue continue
@ -391,7 +395,10 @@ func LoadConfig(filename string) (config *Config, err error) {
if len(logConfig.Types) < 1 { if len(logConfig.Types) < 1 {
return nil, errors.New("Logger has no types to log") return nil, errors.New("Logger has no types to log")
} }
newLogConfigs = append(newLogConfigs, logConfig)
} }
config.Logging = newLogConfigs
return config, nil return config, nil
} }

View File

@ -34,6 +34,12 @@ var (
"error": LogError, "error": LogError,
"errors": LogError, "errors": LogError,
} }
logLevelDisplayNames = map[LogLevel]string{
LogDebug: "debug",
LogInfo: "info",
LogWarn: "warning",
LogError: "error",
}
) )
// ClientLogger is a logger dedicated to a single client. This is a convenience class that // ClientLogger is a logger dedicated to a single client. This is a convenience class that
@ -77,7 +83,7 @@ func NewLogger(config []LoggingConfig) (*Logger, error) {
ExcludedTypes: logConfig.ExcludedTypes, ExcludedTypes: logConfig.ExcludedTypes,
} }
if sLogger.MethodFile.Enabled { if sLogger.MethodFile.Enabled {
file, err := os.OpenFile(sLogger.MethodFile.Filename, os.O_APPEND, 0666) file, err := os.OpenFile(sLogger.MethodFile.Filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
if err != nil { if err != nil {
return nil, fmt.Errorf("Could not open log file %s [%s]", sLogger.MethodFile.Filename, err.Error()) return nil, fmt.Errorf("Could not open log file %s [%s]", sLogger.MethodFile.Filename, err.Error())
} }
@ -85,6 +91,7 @@ func NewLogger(config []LoggingConfig) (*Logger, error) {
sLogger.MethodFile.File = file sLogger.MethodFile.File = file
sLogger.MethodFile.Writer = writer sLogger.MethodFile.Writer = writer
} }
logger.loggers = append(logger.loggers, sLogger)
} }
return &logger, nil return &logger, nil
@ -132,7 +139,7 @@ func (logger *SingleLogger) Log(level LogLevel, logType, object, message string)
} }
// assemble full line // assemble full line
fullString := fmt.Sprintf("%s : %s : %s : %s", time.Now().UTC().Format("2006-01-02T15:04:05.999Z"), logType, object, message) fullString := fmt.Sprintf("%s : %s : %s : %s : %s", time.Now().UTC().Format("2006-01-02T15:04:05Z"), logLevelDisplayNames[level], logType, object, message)
// output // output
if logger.MethodSTDERR { if logger.MethodSTDERR {

View File

@ -136,18 +136,17 @@ type clientConn struct {
} }
// NewServer returns a new Oragono server. // NewServer returns a new Oragono server.
func NewServer(configFilename string, config *Config) *Server { func NewServer(configFilename string, config *Config, logger *Logger) (*Server, error) {
casefoldedName, err := Casefold(config.Server.Name) casefoldedName, err := Casefold(config.Server.Name)
if err != nil { if err != nil {
log.Println(fmt.Sprintf("Server name isn't valid: [%s]", config.Server.Name), err.Error()) return nil, fmt.Errorf("Server name isn't valid [%s]: %s", config.Server.Name, err.Error())
return nil
} }
// startup check that we have HELP entries for every command // startup check that we have HELP entries for every command
for name := range Commands { for name := range Commands {
_, exists := Help[strings.ToLower(name)] _, exists := Help[strings.ToLower(name)]
if !exists { if !exists {
log.Fatal("Help entry does not exist for ", name) return nil, fmt.Errorf("Help entry does not exist for command %s", name)
} }
} }
@ -162,20 +161,20 @@ func NewServer(configFilename string, config *Config) *Server {
operClasses, err := config.OperatorClasses() operClasses, err := config.OperatorClasses()
if err != nil { if err != nil {
log.Fatal("Error loading oper classes:", err.Error()) return nil, fmt.Errorf("Error loading oper classes: %s", err.Error())
} }
opers, err := config.Operators(operClasses) opers, err := config.Operators(operClasses)
if err != nil { if err != nil {
log.Fatal("Error loading operators:", err.Error()) return nil, fmt.Errorf("Error loading operators: %s", err.Error())
} }
connectionLimits, err := NewConnectionLimits(config.Server.ConnectionLimits) connectionLimits, err := NewConnectionLimits(config.Server.ConnectionLimits)
if err != nil { if err != nil {
log.Fatal("Error loading connection limits:", err.Error()) return nil, fmt.Errorf("Error loading connection limits: %s", err.Error())
} }
connectionThrottle, err := NewConnectionThrottle(config.Server.ConnectionThrottle) connectionThrottle, err := NewConnectionThrottle(config.Server.ConnectionThrottle)
if err != nil { if err != nil {
log.Fatal("Error loading connection throttler:", err.Error()) return nil, fmt.Errorf("Error loading connection throttler: %s", err.Error())
} }
server := &Server{ server := &Server{
@ -204,6 +203,7 @@ func NewServer(configFilename string, config *Config) *Server {
}, },
}, },
listeners: make(map[string]ListenerInterface), listeners: make(map[string]ListenerInterface),
logger: logger,
monitoring: make(map[string][]Client), monitoring: make(map[string][]Client),
name: config.Server.Name, name: config.Server.Name,
nameCasefolded: casefoldedName, nameCasefolded: casefoldedName,
@ -221,7 +221,7 @@ func NewServer(configFilename string, config *Config) *Server {
// open data store // open data store
db, err := buntdb.Open(config.Datastore.Path) db, err := buntdb.Open(config.Datastore.Path)
if err != nil { if err != nil {
log.Fatal(fmt.Sprintf("Failed to open datastore: %s", err.Error())) return nil, fmt.Errorf("Failed to open datastore: %s", err.Error())
} }
server.store = db server.store = db
@ -229,7 +229,7 @@ func NewServer(configFilename string, config *Config) *Server {
err = server.store.View(func(tx *buntdb.Tx) error { err = server.store.View(func(tx *buntdb.Tx) error {
version, _ := tx.Get(keySchemaVersion) version, _ := tx.Get(keySchemaVersion)
if version != latestDbSchema { if version != latestDbSchema {
log.Println(fmt.Sprintf("Database must be updated. Expected schema v%s, got v%s.", latestDbSchema, version)) logger.Log(LogError, "startup", "server", fmt.Sprintf("Database must be updated. Expected schema v%s, got v%s.", latestDbSchema, version))
return errDbOutOfDate return errDbOutOfDate
} }
return nil return nil
@ -237,7 +237,7 @@ func NewServer(configFilename string, config *Config) *Server {
if err != nil { if err != nil {
// close the db // close the db
db.Close() db.Close()
return nil return nil, errDbOutOfDate
} }
// load *lines // load *lines
@ -261,7 +261,7 @@ func NewServer(configFilename string, config *Config) *Server {
return nil return nil
}) })
if err != nil { if err != nil {
log.Fatal(fmt.Sprintf("Could not load salt: %s", err.Error())) return nil, fmt.Errorf("Could not load salt: %s", err.Error())
} }
if config.Server.MOTD != "" { if config.Server.MOTD != "" {
@ -309,11 +309,11 @@ func NewServer(configFilename string, config *Config) *Server {
// start API if enabled // start API if enabled
if server.restAPI.Enabled { if server.restAPI.Enabled {
Log.info.Printf("%s rest API started on %s .", server.name, server.restAPI.Listen) logger.Log(LogInfo, "startup", "server", fmt.Sprintf("%s rest API started on %s.", server.name, server.restAPI.Listen))
server.startRestAPI() server.startRestAPI()
} }
return server return server, nil
} }
// setISupport sets up our RPL_ISUPPORT reply. // setISupport sets up our RPL_ISUPPORT reply.
@ -378,7 +378,7 @@ func (server *Server) Shutdown() {
server.clients.ByNickMutex.RUnlock() server.clients.ByNickMutex.RUnlock()
if err := server.store.Close(); err != nil { if err := server.store.Close(); err != nil {
Log.error.Println("Server.Shutdown store.Close: error:", err) server.logger.Log(LogError, "shutdown", "db", fmt.Sprintln("Server.Shutdown store.Close: error:", err))
} }
} }
@ -398,7 +398,7 @@ func (server *Server) Run() {
// eventually we expect to use HUP to reload config // eventually we expect to use HUP to reload config
err := server.rehash() err := server.rehash()
if err != nil { if err != nil {
Log.error.Println("Failed to rehash:", err.Error()) server.logger.Log(LogError, "rehash", "server", fmt.Sprintln("Failed to rehash:", err.Error()))
} }
case conn := <-server.newConns: case conn := <-server.newConns:
@ -497,7 +497,7 @@ func (server *Server) createListener(addr string, tlsMap map[string]*tls.Config)
server.listeners[addr] = li server.listeners[addr] = li
// start listening // start listening
Log.info.Printf("%s listening on %s using %s.", server.name, addr, tlsString) server.logger.Log(LogInfo, "listeners", "listener", fmt.Sprintf("listening on %s using %s.", addr, tlsString))
// setup accept goroutine // setup accept goroutine
go func() { go func() {
@ -549,7 +549,7 @@ func (server *Server) createListener(addr string, tlsMap map[string]*tls.Config)
server.listenerUpdateMutex.Unlock() server.listenerUpdateMutex.Unlock()
// print notice // print notice
Log.info.Printf("%s updated listener %s using %s.", server.name, addr, tlsString) server.logger.Log(LogInfo, "listeners", "listener", fmt.Sprintf("updated listener %s using %s.", addr, tlsString))
} }
default: default:
// no events waiting for us, fall-through and continue // no events waiting for us, fall-through and continue
@ -565,7 +565,7 @@ func (server *Server) createListener(addr string, tlsMap map[string]*tls.Config)
func (server *Server) wslisten(addr string, tlsMap map[string]*TLSListenConfig) { func (server *Server) wslisten(addr string, tlsMap map[string]*TLSListenConfig) {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" { if r.Method != "GET" {
Log.error.Printf("%s method not allowed", server.name) server.logger.Log(LogError, "ws", addr, fmt.Sprintf("%s method not allowed", r.Method))
return return
} }
@ -578,7 +578,7 @@ func (server *Server) wslisten(addr string, tlsMap map[string]*TLSListenConfig)
ws, err := upgrader.Upgrade(w, r, nil) ws, err := upgrader.Upgrade(w, r, nil)
if err != nil { if err != nil {
Log.error.Printf("%s websocket upgrade error: %s", server.name, err) server.logger.Log(LogError, "ws", addr, fmt.Sprintf("%s websocket upgrade error: %s", server.name, err))
return return
} }
@ -596,7 +596,7 @@ func (server *Server) wslisten(addr string, tlsMap map[string]*TLSListenConfig)
if listenTLS { if listenTLS {
tlsString = "TLS" tlsString = "TLS"
} }
Log.info.Printf("%s websocket listening on %s using %s.", server.name, addr, tlsString) server.logger.Log(LogInfo, "listeners", "listener", fmt.Sprintf("websocket listening on %s using %s.", addr, tlsString))
if listenTLS { if listenTLS {
err = http.ListenAndServeTLS(addr, config.Cert, config.Key, nil) err = http.ListenAndServeTLS(addr, config.Cert, config.Key, nil)
@ -604,7 +604,7 @@ func (server *Server) wslisten(addr string, tlsMap map[string]*TLSListenConfig)
err = http.ListenAndServe(addr, nil) err = http.ListenAndServe(addr, nil)
} }
if err != nil { if err != nil {
Log.error.Printf("%s listenAndServe (%s) error: %s", server.name, tlsString, err) server.logger.Log(LogError, "listeners", "listener", fmt.Sprintf("listenAndServe error [%s]: %s", tlsString, err))
} }
}() }()
} }

View File

@ -43,6 +43,11 @@ Options:
log.Fatal("Config file did not load successfully:", err.Error()) log.Fatal("Config file did not load successfully:", err.Error())
} }
logger, err := irc.NewLogger(config.Logging)
if err != nil {
log.Fatal("Logger did not load successfully:", err.Error())
}
if arguments["genpasswd"].(bool) { if arguments["genpasswd"].(bool) {
fmt.Print("Enter Password: ") fmt.Print("Enter Password: ")
bytePassword, err := terminal.ReadPassword(int(syscall.Stdin)) bytePassword, err := terminal.ReadPassword(int(syscall.Stdin))
@ -52,7 +57,7 @@ Options:
password := string(bytePassword) password := string(bytePassword)
encoded, err := irc.GenerateEncodedPassword(password) encoded, err := irc.GenerateEncodedPassword(password)
if err != nil { if err != nil {
log.Fatalln("encoding error:", err) log.Fatal("encoding error:", err.Error())
} }
fmt.Print("\n") fmt.Print("\n")
fmt.Println(encoded) fmt.Println(encoded)
@ -84,16 +89,15 @@ Options:
} }
} }
} else if arguments["run"].(bool) { } else if arguments["run"].(bool) {
irc.Log.SetLevel(config.Server.Log)
rand.Seed(time.Now().UTC().UnixNano()) rand.Seed(time.Now().UTC().UnixNano())
server := irc.NewServer(configfile, config) server, err := irc.NewServer(configfile, config, logger)
if server == nil { if err != nil {
log.Println("Could not load server") logger.Log(irc.LogError, "startup", "server", fmt.Sprintf("Could not load server: %s", err.Error()))
return return
} }
if !arguments["--quiet"].(bool) { if !arguments["--quiet"].(bool) {
log.Println(fmt.Sprintf("Oragono v%s running", irc.SemVer)) logger.Log(irc.LogInfo, "startup", "server", fmt.Sprintf("Oragono v%s running", irc.SemVer))
defer log.Println(irc.SemVer, "exiting") defer logger.Log(irc.LogInfo, "shutdown", "server", fmt.Sprintf("Oragono v%s exiting", irc.SemVer))
} }
server.Run() server.Run()
} }