Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Fix/57 need logging in space cloud #508

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package config

import (
"github.com/spaceuptech/space-cloud/utils"
)

// Config holds the entire configuration
type Config struct {
Projects []*Project `json:"projects" yaml:"projects"` // The key here is the project id
Expand Down Expand Up @@ -49,6 +53,7 @@ type SSL struct {
// Modules holds the config of all the modules of that environment
type Modules struct {
Crud Crud `json:"crud" yaml:"crud"`
Logging Log `json:"logging" yaml:"logging"`
Auth Auth `json:"auth" yaml:"auth"`
Services *ServicesModule `json:"services" yaml:"services"`
FileStore *FileStore `json:"fileStore" yaml:"fileStore"`
Expand All @@ -66,6 +71,15 @@ type CrudStub struct {
Enabled bool `json:"enabled" yaml:"enabled"`
}

//Log holds the mapping of loggers
type Log map[string]*LogConf

//LogConf holds the config of a logger
type LogConf struct {
Levels []utils.LogLevel `json:"levels" yaml:"levels"`
Enabled bool `json:"enabled" yaml:"enabled"`
}

// TableRule contains the config at the collection level
type TableRule struct {
IsRealTimeEnabled bool `json:"isRealtimeEnabled" yaml:"isRealtimeEnabled"`
Expand Down
8 changes: 8 additions & 0 deletions utils/LoggerType.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package utils

type LoggerType string

const(
StdOut LoggerType = "stdOut"
)

106 changes: 106 additions & 0 deletions utils/logging/logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package logging

import (
"errors"
"fmt"
"github.com/spaceuptech/space-cloud/config"
"github.com/spaceuptech/space-cloud/utils"
"github.com/spaceuptech/space-cloud/utils/logging/stdOut"
"sync"
)

type Module struct {
sync.RWMutex
loggers map[string]Logger
}

type Logger interface {
Debug(msg string, data map[string]interface{}) error
Info(msg string, data map[string]interface{}) error
Warning(msg string, data map[string]interface{}) error
Error(msg string, data map[string]interface{}) error
Close() error
}

func Init() *Module {
return &Module{loggers: make(map[string]Logger)}
}

func initLogger(logType utils.LoggerType, levels []utils.LogLevel, enabled bool) (Logger, error){

switch logType {
case utils.StdOut:
return stdOut.Init(levels, enabled)
}

return nil, errors.New("given loggerType could not be resolved")
}

func (m *Module) SetConfig(log config.Log) error {
m.Lock()
defer m.Unlock()

// Close the previous loggers connections
for _, v := range m.loggers {
v.Close()
}
m.loggers = make(map[string]Logger, len(log))

// Create new logger
for k, v := range log {
c, err := initLogger(utils.LoggerType(k), v.Levels, v.Enabled)
m.loggers[k] = c

if err != nil {
fmt.Println("Error creating Logger with key", k)
}
}
return nil
}

func (m *Module) Debug(msg string, data map[string]interface{}) error {
for _, v := range m.loggers{
err := v.Debug(msg, data); if err != nil {
return err
}
}

return nil
}
func (m Module) Info(msg string, data map[string]interface{}) error{
for _, v := range m.loggers{
err :=v.Info(msg, data); if err != nil {
return err
}
}

return nil
}
func (m Module) Warning(msg string, data map[string]interface{}) error{
for _, v := range m.loggers{
err :=v.Warning(msg, data); if err != nil {
return err
}
}

return nil
}
func (m Module) Error(msg string, data map[string]interface{}) error{
for _, v := range m.loggers{
err :=v.Error(msg, data); if err != nil {
return err
}
}

return nil
}

func (m Module) Close() error{
for _, v := range m.loggers{
err :=v.Close(); if err != nil {
return err
}
}

return nil
}
41 changes: 41 additions & 0 deletions utils/logging/stdOut/stdOut.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package stdOut

import (
"fmt"
"github.com/spaceuptech/space-cloud/utils"
)

type StdOut struct {
enabled bool
levels []utils.LogLevel
}

func Init(levels []utils.LogLevel, enabled bool) (*StdOut, error){
stdOutRef := &StdOut{
enabled: enabled,
levels: levels,
}
return stdOutRef, nil
}


func (stdOutRef StdOut) Debug(msg string, data map[string]interface{}) error {
fmt.Println(utils.LogLevel(utils.DEBUG).String(), ":", msg, data)
return nil
}
func (stdOutRef StdOut) Info(msg string, data map[string]interface{}) error{
fmt.Println(utils.LogLevel(utils.INFO).String(), ":", msg, data)
return nil
}
func (stdOutRef StdOut) Warning(msg string, data map[string]interface{}) error{
fmt.Println(utils.LogLevel(utils.WARNING).String(), ":", msg, data)
return nil
}
func (stdOutRef StdOut) Error(msg string, data map[string]interface{}) error{
fmt.Println(utils.LogLevel(utils.ERROR).String(), ":", msg, data)
return nil
}

func (stdOutRef StdOut) Close () error{
return nil
}
28 changes: 28 additions & 0 deletions utils/loglevel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package utils

type LogLevel int

const(
DEBUG LogLevel = iota
INFO LogLevel = iota
WARNING LogLevel = iota
ERROR LogLevel = iota
)

func (level LogLevel) isValid() bool{
return !(level < DEBUG || level > ERROR)
}

func (level LogLevel) String() string{
names := [...]string{
"Debug",
"Info",
"Warning",
"Error" }

if level < DEBUG || level > ERROR {
return "Unknown"
}

return names[level]
}
12 changes: 11 additions & 1 deletion utils/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package server

import (
"fmt"
"github.com/spaceuptech/space-cloud/utils/logging"
"log"
"net/http"
"strconv"
Expand Down Expand Up @@ -34,6 +35,7 @@ type Server struct {
routerSecure *mux.Router
auth *auth.Module
crud *crud.Module
logging *logging.Module
user *userman.Module
file *filestore.Module
functions *functions.Module
Expand All @@ -55,6 +57,7 @@ func New(nodeID, clusterID string, isConsulEnabled, removeProjectScope bool, met

// Create the fundamental modules
c := crud.Init(removeProjectScope)
l := logging.Init()

m, err := metrics.New(nodeID, metricsConfig)
if err != nil {
Expand Down Expand Up @@ -93,7 +96,7 @@ func New(nodeID, clusterID string, isConsulEnabled, removeProjectScope bool, met

fmt.Println("Creating a new server with id", nodeID)

return &Server{nodeID: nodeID, router: r, routerSecure: r2, auth: a, crud: c,
return &Server{nodeID: nodeID, router: r, routerSecure: r2, auth: a, crud: c, logging: l,
user: u, file: f, syncMan: syncMan, adminMan: adminMan, metrics: m,
functions: fn, realtime: rt, configFilePath: utils.DefaultConfigFilePath,
eventing: e, graphql: graphqlMan}, nil
Expand All @@ -116,6 +119,7 @@ func (s *Server) Start(disableMetrics bool, port int) error {
corsObj := utils.CreateCorsObject()

fmt.Println("Starting http server on port: " + strconv.Itoa(port))
s.logging.Debug("Test-Message!!! Logging works", nil)

if s.ssl != nil && s.ssl.Enabled {
handler := corsObj.Handler(s.routerSecure)
Expand Down Expand Up @@ -156,6 +160,12 @@ func (s *Server) LoadConfig(config *config.Config) error {

p := config.Projects[0]

// Set the configuration for the logging module
if err := s.logging.SetConfig(p.Modules.Logging); err != nil {
log.Println("Error in logging module config: ", err)
return err
}

// Always set the config of the crud module first
// Set the configuration for the crud module
if err := s.crud.SetConfig(p.ID, p.Modules.Crud); err != nil {
Expand Down