Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supporting Custom Logger Class #2511

Closed
jchristie55332 opened this issue Dec 10, 2020 · 6 comments · Fixed by #2725 or #2770
Closed

Supporting Custom Logger Class #2511

jchristie55332 opened this issue Dec 10, 2020 · 6 comments · Fixed by #2725 or #2770

Comments

@jchristie55332
Copy link
Contributor

Is your feature request related to a problem? Please describe.

It is possible to have custom loggers that allow for json log aggregation as well as formatting and custom attributes.

Without json log aggregation results in poorly formatted logs and duplication of log formatting rules.

Describe the solution you'd like

It would be great to pass a custom logging class to puma that would then be logged to.

I would expect this to result in a new method in the DSL.

@nateberkopec
Copy link
Member

Does log_formatter do what you want?

e.g.

log_formatter do |str|
  "[#{Process.pid}] [#{Socket.gethostname}] #{Time.now}: #{str}"
end

@jchristie55332
Copy link
Contributor Author

jchristie55332 commented Dec 11, 2020

Does log_formatter do what you want?

e.g.

log_formatter do |str|
  "[#{Process.pid}] [#{Socket.gethostname}] #{Time.now}: #{str}"
end

No. Using log_formatter still results in the formatted string being output to STDOUT.

I suggest that we support the use of a custom logger for outputting of log lines

puma/lib/puma/events.rb

Lines 62 to 69 in 503b1cd

# Write +str+ to +@stdout+
#
def log(str)
@stdout.puts format(str) if @stdout.respond_to? :puts
@stdout.flush unless @stdout.sync
rescue Errno::EPIPE
end
I was also thinking that we could include for the ErrorLogger

@kuei0221
Copy link
Contributor

How about stdout_redirect ? It will save the STDOUT to the file you want.

puma/lib/puma/dsl.rb

Lines 388 to 399 in 503b1cd

# Redirect +STDOUT+ and +STDERR+ to files specified. The +append+ parameter
# specifies whether the output is appended, the default is +false+.
#
# @example
# stdout_redirect '/app/lolcat/log/stdout', '/app/lolcat/log/stderr'
# @example
# stdout_redirect '/app/lolcat/log/stdout', '/app/lolcat/log/stderr', true
def stdout_redirect(stdout=nil, stderr=nil, append=false)
@options[:redirect_stdout] = stdout
@options[:redirect_stderr] = stderr
@options[:redirect_append] = append
end

@alexeevit
Copy link
Contributor

alexeevit commented May 24, 2021

So, do you expect to use it like something like this?

# config/puma.rb
...
custom_logger_class CustomLogger
...

# custom_logger.rb
class CustomLogger
  def call(level, message)
    ...
  end
end

@nateberkopec
Copy link
Member

Generally with a custom logger I would expect a config interface like:

logger Logger.new(STDOUT)

@svoop
Copy link

svoop commented Apr 21, 2023

For reference, the interface is a little different from what @nateberkopec has suggested:

custom_logger Logger.new(STDOUT)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants