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

fix: set logging back to stdout to access logs in kubectl #607

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
22 changes: 4 additions & 18 deletions backend/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,19 @@ import (
)

func main() {
file := os.Stdout
defer file.Close()
if err := godotenv.Load(); err != nil {
log.Info("no .env file found, using default env variables")
}
env := os.Getenv("APP_ENV")
testing := (env == "testing")
initLogging(env, file)
initLogging()
newServer := server.NewServer(testing)
newServer.ListenAndServe()
}

func initLogging(env string, file *os.File) {
var err error
prod := (env == "prod" || env == "production")
if prod {
file, err = os.OpenFile("logs/server.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("Failed to open log file: %v", err)
}
log.SetFormatter(&log.JSONFormatter{})
} else {
log.SetFormatter(&log.TextFormatter{ForceColors: true})
}
level := parseLogLevel()
log.SetLevel(level)
log.SetOutput(file)
func initLogging() {
log.SetFormatter(&log.JSONFormatter{})
log.SetLevel(parseLogLevel())
}

func parseLogLevel() log.Level {
Expand Down
5 changes: 4 additions & 1 deletion backend/src/handlers/helpful_links_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"net/http"
"net/url"
"strconv"

"github.com/sirupsen/logrus"
)

func (srv *Server) registerLeftMenuRoutes() []routeDef {
Expand Down Expand Up @@ -160,13 +162,14 @@ func (srv *Server) handleFavoriteLink(w http.ResponseWriter, r *http.Request, lo
if err != nil {
return newDatabaseServiceError(err)
}
if _, err := srv.Db.FavoriteOpenContent(int(userID), link.OpenContentProviderID, uint(linkID), facilityID); err != nil {
if _, err := srv.Db.FavoriteOpenContent(int(linkID), link.OpenContentProviderID, uint(userID), facilityID); err != nil {
return newDatabaseServiceError(err)
}
return writeJsonResponse(w, http.StatusOK, "Link favorite toggled successfully")
}

func (srv *Server) getFavicon(link string) string {
logrus.Printf("Getting favicon for link %s", link)
baseUrl, err := url.Parse(link)
if err != nil {
return "/ul-icon.png"
Expand Down
2 changes: 1 addition & 1 deletion backend/src/models/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const (
SyncVideoMetadataJob JobType = "sync_video_metadata"
PutVideoMetadataJob JobType = "put_video_metadata"
AddVideosJob JobType = "add_videos"
EveryThreeHours string = "0 */3 * * *"
EveryThreeHours string = "0 6-20 * * *"
EverySundayAt8PM string = "0 20 * * 7"
StatusPending JobStatus = "pending"
StatusRunning JobStatus = "running"
Expand Down
19 changes: 5 additions & 14 deletions backend/tasks/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,31 +54,22 @@ func main() {

func initLogging() {
env := os.Getenv("APP_ENV")
var err error
var file *os.File
if env == "development" {
log.SetFormatter(&log.TextFormatter{})
file = os.Stdout
} else {
log.SetFormatter(&log.JSONFormatter{})
file, err = os.OpenFile("logs/cron.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
level, err := log.ParseLevel(os.Getenv("LOG_LEVEL"))
if err != nil {
log.Error("Failed to log to file, using default stderr")
file = os.Stdout
log.Error("Failed to parse log level, using default info")
level = log.DebugLevel
}
log.SetLevel(level)
}
log.SetOutput(file)
level, err := log.ParseLevel(os.Getenv("LOG_LEVEL"))
if err != nil {
log.Error("Failed to parse log level, using default info")
level = log.DebugLevel
}
log.SetLevel(level)
}

func getCronSchedule(task *models.RunnableTask, hour int) string {
if task.Provider != nil && task.Provider.Type == models.Brightspace {
return fmt.Sprintf("0 0 %d * * 4", hour)
return fmt.Sprintf("0 %d * * 4", hour)
} else if task.Job.Name == string(models.PutVideoMetadataJob) {
return models.EverySundayAt8PM
} else {
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ services:
context: .
dockerfile: provider-middleware/dev.Dockerfile
environment:
- APP_ENV=production
- APP_ENV=dev
- DB_HOST=postgres
- DB_PORT=5432
- DB_USER=unlocked
Expand Down
13 changes: 11 additions & 2 deletions provider-middleware/kiwix.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,24 @@ import (
"fmt"
"io"
"net/http"
"os"
"sync"

"gorm.io/gorm"
)

const (
KiwixCatalogUrl = "/catalog/v2/entries?lang=eng&start=1&count="
MaxLibraries = 1000
)

var maxLibraries = sync.OnceValue(func() int {
if os.Getenv("APP_ENV") == "dev" {
return 10
} else {
return 1000
}
})

type KiwixService struct {
OpenContentProviderId uint
Url string
Expand All @@ -26,7 +35,7 @@ type KiwixService struct {
}

func NewKiwixService(openContentProvider *models.OpenContentProvider, params *map[string]interface{}) *KiwixService {
url := fmt.Sprintf("%s%s%d", openContentProvider.Url, KiwixCatalogUrl, MaxLibraries)
url := fmt.Sprintf("%s%s%d", openContentProvider.Url, KiwixCatalogUrl, maxLibraries())
client := http.Client{}
jobID := (*params)["job_id"].(string)
return &KiwixService{
Expand Down
32 changes: 1 addition & 31 deletions provider-middleware/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,37 +103,7 @@ func main() {

func initLogging() *log.Logger {
var logger = log.New()
var file *os.File
var err error
env := os.Getenv("APP_ENV")
if env == "production" || env == "prod" {
file, err = os.OpenFile("logs/provider-middleware.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
file, err = os.Create("logs/provider-middleware.log")
if err != nil {
log.Fatalf("Failed to create log file: %v", err)
}
}
} else {
file = os.Stdout
}
logger.SetOutput(file)
logger.SetFormatter(&log.JSONFormatter{})
if os.Getenv("LOG_LEVEL") == "" {
switch env {
case "prod", "production":
log.SetLevel(log.InfoLevel)
case "dev", "development":
log.SetLevel(log.TraceLevel)
default:
log.SetLevel(log.DebugLevel)
}
} else {
level, err := log.ParseLevel(os.Getenv("LOG_LEVEL"))
if err != nil {
log.SetLevel(log.DebugLevel)
}
logger.SetLevel(level)
}
logger.SetLevel(log.InfoLevel)
return logger
}
Loading