console.warn
s show up as errors
armster15
PROOP

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

2 years ago

3748ea1a-2245-4b3a-b098-d3a49a2b9db7


armster15
PROOP

2 years ago

its nextjs so nodejs


2 years ago

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

1347329440656654377


armster15
PROOP

2 years ago

oh i didnt know .warn goes to stderr


armster15
PROOP

2 years ago

wdym by "log with json" though


armster15
PROOP

2 years ago

but TIL lol

1347329901144965203


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

2 years ago

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


2 years 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.NewJSONHandler(os.Stdout, &slog.HandlerOptions{AddSource: true}))).


armster15
PROOP

2 years ago

interesting


armster15
PROOP

2 years ago

i found a package so im trying it out rn


armster15
PROOP

2 years ago

but what exactly is json logging?


armster15
PROOP

2 years ago

ive never heard of it until now


armster15
PROOP

2 years ago

is it just writing a json string to stdout


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


2 years ago

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


armster15
PROOP

2 years ago

yay it worked ty


armster15
PROOP

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

2 years ago

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

1347348032244547664


2 years ago

interesting approach


2 years ago

!s


Status changed to Solved brody • over 1 year ago


Welcome!

Sign in to your Railway account to join the conversation.

Loading...