R
Railway5mo ago
sowing

go logging registers as errors

project id: 271ed33d-be6b-40ce-96ba-504a94362dac howdy y'all. I'm hosting a go app on railway and it's been great but whenever I use go's default logger or charm log, all of my logs show up as errors instead of info. I have tried modifying the default logger's output to ensure it's not printing to standard err but it's still categorizing all of my logging as errors. Just wondering if anyone has any insight as to why this is happening.
Solution:
but making a new logger with stdout as its output seems to fix the problem
Jump to solution
36 Replies
Percy
Percy5mo ago
Project ID: 271ed33d-be6b-40ce-96ba-504a94362dac
Brody
Brody5mo ago
you are on the right track, sending logs to stdout instead of stderr would print them as info, can you please provide a minimal reproducible example for me to test
sowing
sowingOP5mo ago
yeah i'll whip one up when i get back to my computer
Brody
Brody5mo ago
sounds good!
sowing
sowingOP5mo ago
ok i think this should do it
package main

import (
"os"

"github.com/charmbracelet/log"
)

func main() {
log.Default().setOutput(os.Stdout)

port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
log.Infof("Listening on port: %s", port)
}
package main

import (
"os"

"github.com/charmbracelet/log"
)

func main() {
log.Default().setOutput(os.Stdout)

port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
log.Infof("Listening on port: %s", port)
}
Brody
Brody5mo ago
what version of go do you use
sowing
sowingOP5mo ago
i tried both log.Default().setOutput() and log.setOutput() and both seem to send to stderr anyways 1.22
Brody
Brody5mo ago
is cmp.Or a thing in that version?
sowing
sowingOP5mo ago
it is indeed
Brody
Brody5mo ago
little tip but it has nothing to do with logs - port := cmp.Or(os.Getenv("PORT"), "8080") saves 3 lines haha
sowing
sowingOP5mo ago
that's pretty nice. never used that before lol
Brody
Brody5mo ago
okay ill get to testing now
sowing
sowingOP5mo ago
thanks so much!
Brody
Brody5mo ago
well not off to a good start
No description
sowing
sowingOP5mo ago
oh that's totally my bad no lsp in discord code blocks capitals for public methods is right. I've been writing too much rust okay so it looks like you just can't set the default logger's output to stdout
Brody
Brody5mo ago
i was just about to say that lol
Solution
sowing
sowing5mo ago
but making a new logger with stdout as its output seems to fix the problem
sowing
sowingOP5mo ago
so i'll just have to make that work should be fine
Brody
Brody5mo ago
i was also going to say that lol
sowing
sowingOP5mo ago
thanks so much for your time on this lol
Brody
Brody5mo ago
as a go dev myself, may i ask why charm?
sowing
sowingOP5mo ago
pretty colors if i'm being serious it's mostly that i appreciate them having a lot of different log levels that are visually differentiated instead of just either crashing the program or not
Brody
Brody5mo ago
are you planning on using the json logger?
sowing
sowingOP5mo ago
i wasn't aware there was going to be one
Brody
Brody5mo ago
this was all done with stdlib slog
No description
Brody
Brody5mo ago
this is what can be done with json logging on railway
sowing
sowingOP5mo ago
oh that's pretty interesting. I might look into implementing that down the line this is just my personal site so right now I'm just trying to do basic request logging middleware that doesn't turn my whole log page red lol
Brody
Brody5mo ago
sounds good
sowing
sowingOP5mo ago
thanks for helping out again :)
Brody
Brody5mo ago
no problem!
sowing
sowingOP5mo ago
interesting postscript to this: it looks like my original fix actually was working for some logs but not others. I'll have to do some digging to see what's different between them
Brody
Brody5mo ago
charm doing some weird stuff then eh
sowing
sowingOP5mo ago
i figured it out and this is incredibly silly my original fix totally worked: it redirected all of the charm logs to stdout instead of stderr but my logging middleware was using stdlib log instead of charm log, so it didn't apply to those
Brody
Brody5mo ago
haha okay that's funny
sowing
sowingOP5mo ago
just pushed the change and everything is showing up blue!
Brody
Brody5mo ago
awesome
Want results from more Discord servers?
Add your server