go logging registers as errors
jkellogg01
HOBBYOP

2 years ago

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.

45 Replies

2 years 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


jkellogg01
HOBBYOP

2 years ago

yeah i'll whip one up when i get back to my computer


2 years ago

sounds good!


jkellogg01
HOBBYOP

2 years 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)
}

2 years ago

what version of go do you use


jkellogg01
HOBBYOP

2 years ago

i tried both log.Default().setOutput() and log.setOutput() and both seem to send to stderr anyways


jkellogg01
HOBBYOP

2 years ago

1.22


2 years ago

is cmp.Or a thing in that version?


jkellogg01
HOBBYOP

2 years ago

it is indeed


2 years ago

little tip but it has nothing to do with logs - port := cmp.Or(os.Getenv("PORT"), "8080")
saves 3 lines haha


jkellogg01
HOBBYOP

2 years ago

that's pretty nice. never used that before lol


2 years ago

okay ill get to testing now


jkellogg01
HOBBYOP

2 years ago

thanks so much!


2 years ago

well not off to a good start

1261584026741768200


jkellogg01
HOBBYOP

2 years ago

oh that's totally my bad


jkellogg01
HOBBYOP

2 years ago

no lsp in discord code blocks


jkellogg01
HOBBYOP

2 years ago

capitals for public methods is right. I've been writing too much rust


jkellogg01
HOBBYOP

2 years ago

okay so it looks like you just can't set the default logger's output to stdout


2 years ago

i was just about to say that lol


jkellogg01
HOBBYOP

2 years ago

but making a new logger with stdout as its output seems to fix the problem


jkellogg01
HOBBYOP

2 years ago

so i'll just have to make that work


jkellogg01
HOBBYOP

2 years ago

should be fine


2 years ago

i was also going to say that lol


jkellogg01
HOBBYOP

2 years ago

thanks so much for your time on this lol


2 years ago

as a go dev myself, may i ask why charm?


jkellogg01
HOBBYOP

2 years ago

pretty colors


jkellogg01
HOBBYOP

2 years ago

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


2 years ago

are you planning on using the json logger?


jkellogg01
HOBBYOP

2 years ago

i wasn't aware there was going to be one


2 years ago

this was all done with stdlib slog

1261586182417158100


2 years ago

this is what can be done with json logging on railway


jkellogg01
HOBBYOP

2 years ago

oh that's pretty interesting. I might look into implementing that down the line


jkellogg01
HOBBYOP

2 years ago

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


2 years ago

sounds good


jkellogg01
HOBBYOP

2 years ago

thanks for helping out again :)


2 years ago

no problem!


jkellogg01
HOBBYOP

2 years 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


2 years ago

charm doing some weird stuff then eh


jkellogg01
HOBBYOP

2 years ago

i figured it out


jkellogg01
HOBBYOP

2 years ago

and this is incredibly silly


jkellogg01
HOBBYOP

2 years ago

my original fix totally worked: it redirected all of the charm logs to stdout instead of stderr


jkellogg01
HOBBYOP

2 years ago

but my logging middleware was using stdlib log instead of charm log, so it didn't apply to those


2 years ago

haha okay that's funny


jkellogg01
HOBBYOP

2 years ago

just pushed the change and everything is showing up blue!


2 years ago

awesome


Loading...