Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 356 Vote(s) - 3.51 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I log correctly inside my Ruby gem?

#1
Currently I'm using `puts`, but I'm sure that's not the correct answer. How do I correctly setup a logger, inside my gem, to output my internal logging instead of `puts`?
Reply

#2
You can keep the logger in your top-level module. Allow user's to set their own logger but provide a reasonable default for those who don't care to deal with logging. For e.g.

module MyGem
class << self
attr_writer :logger

def logger
@logger ||= Logger.new($stdout).tap do |log|
log.progname = self.name
end
end
end
end

Then, anywhere within your gem code you can access the logger. For e.g.

class MyGem::SomeClass

def some_method
# ...
MyGem.logger.info 'some info'
end

end

References:

- [Using the Ruby Logger][1]
- [Logger][2]


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#3
I think the easiest approach is to use it this way

Rails.logger.info "hello"
Reply

#4
A little example:

gem 'log4r'
require 'log4r'

class MyClass
def initialize(name)

@log = Log4r::Logger.new(name)
#Add outputter
#~ log.outputters << Log4r::FileOutputter.new('log_file', :filename => 'mini_example.log', :level => Log4r::ALL )
log.outputters << Log4r::StdoutOutputter.new('log_stdout') #, :level => Log4r::WARN )
#~ log.outputters << Log4r::StderrOutputter.new('log_stderr', :level => Log4r::ERROR)

@log.level = Log4r::INFO
@log.info("Creation")
#~ @log.level = Log4r::OFF
end
attr_reader :log
def myfunction(*par)
@log.debug("myfunction is called")
@log.warn("myfunction: No parameter") if par.empty?
end
end

x = MyClass.new('x')
x.myfunction

y = MyClass.new('y')
y.myfunction
y.myfunction(:a)
y.log.level = Log4r::DEBUG
y.myfunction(:a)


During initialization you create a Logger (`@log`). In your methods you call the logger.

With `@log.level=` (or `MyClass#log.level=`) you can influence, which messages are used.

You can use different outputters (in my example I log to STDOUT). You can also mix outputters (e.g. STDOUT with warnings, each data (including DEBUG) to a file...)


Reply

#5
The most flexible approach for users of your gem is to let them provide a logger rather than setting it up inside the gem. At its simplest this could be

class MyGem
class << self
attr_accessor :logger
end
end

You then use `MyGem.logger.info "hello"` to log messages from your gem (you might want to wrap it in a utility method that tests whether a logger is set at all)

Users of your gem can then control where messages get logged to (a file, syslog, stdout, etc...)
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through