falcon icon indicating copy to clipboard operation
falcon copied to clipboard

Use a Custom Logger instead of Async Logger

Open fabirydel opened this issue 4 years ago • 8 comments

Is is possible to use a custom logger instead of the default Async Logger? I have been struggling to find a way to achieve this

fabirydel avatar May 07 '21 17:05 fabirydel

What problem are you trying to solve? What is a custom logger.

ioquatix avatar May 07 '21 21:05 ioquatix

I need to change the format of the outgoing messages, instead of outputting a string like "info: Starting server [pid=33039] [2021-05-08 08:18:13 +0100]", I need a Json:

{
  severity: 'INFO',
  message: 'Starting server',
  timestamp: '2021-05-08 08:18:13 +0100',
  pid: 33039,
  other_args: {
    ...
  },
  ....
}

I have a custom logger class that is a subclass of Ruby's Logger that takes care of all the parsing and formatting but can't find a way to tell the app to use it for by default instead of Console::Logger.

I've tried doing this when initializing the app and locally it does seem to work.

Async do
  Async.logger = CustomLogger.new($stdout, level: Logger::INFO)
end

But when I run the app on GCP's CloudRun, which sends any log sent to stdout into CloudLogging, I see plain string again. Perhaps the problem is related to CloudRun and not Falcon directly, I've just not found what it is yet.

Thanks in advance for any help!

fabirydel avatar May 08 '21 07:05 fabirydel

Console logger already does this by default. Just output it to a file (or anything other than a TTY) and it will write JSON. Does that solve your problem?

You might need the latest version of the logger to get this.

ioquatix avatar May 08 '21 10:05 ioquatix

Ok I see, and how do I go about changing the default output of the Console Logger from within the Falcon framework? Is that what I need to do then?

fabirydel avatar May 08 '21 11:05 fabirydel

It automatically picks it up you shouldn't need to do anything. Just make sure you are using the latest version.

ioquatix avatar May 08 '21 12:05 ioquatix

Here is an example: https://github.com/socketry/falcon/runs/2532727154?check_suite_focus=true#step:5:56

GitHub Actions doesn't present a TTY (lol) so it assumes it's logging to a file and outputs JSON.

ioquatix avatar May 08 '21 12:05 ioquatix

Hey, thanks for the help so far, but unfortunately it hasn't solved my problem. I actually need to use a different Logger than Console Logger. I'm shipping my Falcon app's logs to GCP and I need to be able to use Google's CloudLogging logger. Their documentation says to add use Google::Cloud::Logging::Middleware in the Rack middleware configuration code, where would that be in a Falcon app?

If that is not possible, ideally I could just do something like we do on a Rails app, but I haven't figured out how yet:

configuration do |config|
  logger = MyCustomGoogleLogger.new(etc)
end

Thanks again for any help!

fabirydel avatar May 10 '21 11:05 fabirydel

use Google::Cloud::Logging::Middleware in the Rack middleware configuration code

In config.ru.

You can tee the output into a logger using a different output but we don't have any google specific output adapters.

ioquatix avatar May 10 '21 12:05 ioquatix