console.warn
s show up as errors

armster15
PRO

5 months ago

Hi, not sure if I'm doing something wrong on my end but any console.warn shows up as an error in my logs so my logs are just filled with errors and no warnings

Solved

0 Replies

armster15
PRO

5 months ago

3748ea1a-2245-4b3a-b098-d3a49a2b9db7


armster15
PRO

5 months ago

its nextjs so nodejs


5 months ago

because it prints to stderr, instead you would want to log with json for the proper colours

1347329440656654300


armster15
PRO

5 months ago

oh i didnt know .warn goes to stderr


armster15
PRO

5 months ago

wdym by "log with json" though


armster15
PRO

5 months ago

but TIL lol

1347329901144965000


5 months ago

you should use a json logger which will format your logs to something like this:

{"time":"2025-03-06T23:16:18.292397+01:00","level":"INFO","msg":"Synced 4 commands"}

armster15
PRO

5 months ago

so i literally write console.log({"time":"2025-03-06T23:16:18.292397+01:00","level":"INFO","msg":"Synced 4 commands"}) ?


5 months ago

you should be able to configure the logging behavior or use an external package that does exactly that.

for example in go, you can just configure the logger to log json using the built in slog package slog.SetDefault([slog.New](slog.New)([slog.New](slog.New)JSONHandler(os.Stdout, &slog.HandlerOptions{AddSource: true}))).


armster15
PRO

5 months ago

interesting


armster15
PRO

5 months ago

i found a package so im trying it out rn


armster15
PRO

5 months ago

but what exactly is json logging?


armster15
PRO

5 months ago

ive never heard of it until now


armster15
PRO

5 months ago

is it just writing a json string to stdout


5 months ago

> JSON logging is the recording of log entries as structured JSON objects. This approach makes log data easy to parse and analyze with various log management systems, analytics tools, and other software applications.


5 months ago

for example, Railway will check for the "level" key in the json and correctly display it as info, error or warn


armster15
PRO

5 months ago

yay it worked ty


armster15
PRO

5 months ago

for those who might come across this in the future: a quick no dep solution is this:

// Use JSON logging in Railway
// https://discord.com/channels/713503345364697088/1347328038886703269
const util = require("util");
if (process.env.RAILWAY_PROJECT_ID) {
  globalThis.console.warn = (...args) =>
    process.stdout.write(
      JSON.stringify({
        level: "warn",
        msg: args.map((arg) => typeof arg === "string" ? arg : util.inspect(arg)).join(" "),
      }) + "\n"
    );
}

armster15
PRO

5 months ago

now i have a nice sea of warnings 😌 (i should prob remove these im ngl)

1347348032244547600


5 months ago

interesting approach


5 months ago

!s


Status changed to Solved brody 5 months ago