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

Feature Request: File logging relative to rails root only #126

Closed
cshupp1 opened this issue Jan 12, 2016 · 3 comments · Fixed by #154
Closed

Feature Request: File logging relative to rails root only #126

cshupp1 opened this issue Jan 12, 2016 · 3 comments · Fixed by #154

Comments

@cshupp1
Copy link

cshupp1 commented Jan 12, 2016

When deploying to a J2EE server file names can get a little long when using %F in the formatter.

For example, this log message:
[ALWAYS][2016-01-12T10:27:06][/work/ETS/glassfish/glassfish4/glassfish/domains/domain1/applications/ets_tooling/WEB-INF/config/initializers/02_log_initializer.rb][59] Logging started.

Would look better as:
[ALWAYS][2016-01-12T12:57:07][/config/initializers/02_log_initializer.rb][108] Logging started!

opening the logging module and redefining the logging event as follows solves the issue for me (but this will only work on JRuby).

v = $VERBOSE
$VERBOSE = nil
module Logging
  #opening and redefining LogEvent found in log_event.rb

  LogEvent = Struct.new( :logger, :level, :data, :time, :file, :line, :method ) {

    # Regular expression used to parse out caller information
    #
    # * $1 == filename
    # * $2 == line number
    # * $3 == method name (might be nil)
    CALLER_RGXP = %r/([-\.\/\(\)\w]+):(\d+)(?::in `(\w+)')?/o
    #CALLER_INDEX = 2
    CALLER_INDEX = ((defined? JRUBY_VERSION and JRUBY_VERSION > '1.6') or (defined? RUBY_ENGINE and RUBY_ENGINE[%r/^rbx/i])) ? 1 : 2

    def initialize( logger, level, data, caller_tracing )
      f = l = m = ''

      if caller_tracing
        stack = Kernel.caller[CALLER_INDEX]
        return if stack.nil?

        match = CALLER_RGXP.match(stack)
        f = match[1]
        separator = java.io.File.separator
        last = Rails.root.to_s.split(separator).last
        f = f.split(last).last
        l = Integer(match[2])
        m = match[3] unless match[3].nil?
      end

      super(logger, level, data, Time.now, f, l, m)
    end
  }

end  # module Logging
#put warnings back to whatever they were.
$VERBOSE = v
@vanso-hubsi
Copy link

I had the same request and solved it by monkey patching the LogEvent class that returns the filename:

module Logging
  # patch LogEvent class to only return file path relative to rails root instead of full path if set
  module LogEventExtensions
    def file
      self[:file].blank? ? self[:file] : Pathname.new(self[:file]).relative_path_from(::Rails.root).to_s
    end
  end

  class LogEvent
    prepend LogEventExtensions
  end
end

@cshupp1
Copy link
Author

cshupp1 commented Mar 8, 2017

I just made an attempt to upgrade from 2.0.0 to 2.1.0. I cannot find the basepath in the source code anywhere. And obviously my references to it fail.

Thanks,

Cris

@TwP
Copy link
Owner

TwP commented Mar 10, 2017

Ah, my apologies on that one. I just pushed up a new release - version 2.2.0 - that contains the basepath changes you are looking for.

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

Successfully merging a pull request may close this issue.

3 participants