console.warn
s show up as errors
armster15
PROOP

a year 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

21 Replies

armster15
PROOP

a year ago

3748ea1a-2245-4b3a-b098-d3a49a2b9db7


armster15
PROOP

a year ago

its nextjs so nodejs


brody
EMPLOYEE

a year ago

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

1347329440656654300


armster15
PROOP

a year ago

oh i didnt know .warn goes to stderr


armster15
PROOP

a year ago

wdym by "log with json" though


armster15
PROOP

a year ago

but TIL lol

1347329901144965000


uxuz
MODERATOR

a year 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
PROOP

a year ago

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


uxuz
MODERATOR

a year 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
PROOP

a year ago

interesting


armster15
PROOP

a year ago

i found a package so im trying it out rn


armster15
PROOP

a year ago

but what exactly is json logging?


armster15
PROOP

a year ago

ive never heard of it until now


armster15
PROOP

a year ago

is it just writing a json string to stdout


uxuz
MODERATOR

a year 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.


uxuz
MODERATOR

a year ago

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


armster15
PROOP

a year ago

yay it worked ty


armster15
PROOP

a year 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
PROOP

a year ago

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

1347348032244547600


brody
EMPLOYEE

a year ago

interesting approach


brody
EMPLOYEE

a year ago

!s


Status changed to Solved brody 11 months ago


Loading...