I want to have a custom logger for my application, which of course logs to a different file, someone asked a question: Setting up the logger in rails 3
But I want to have a logger which I can call with my own class name like:
StatusLogger.info "something happend!!!"
How can I do this?
Rails makes use of the ActiveSupport::Logger class to write log information. Other loggers, such as Log4r , may also be substituted. By default, each log is created under Rails. root/log/ and the log file is named after the environment in which the application is running.
In a Rails app, logs are stored under the /log folder. In development mode, the development. log file is used & you see log output on the terminal you're running rails server on.
Rails is configured to create separate log files for each of the three default environments: development, test and production. By default it puts these log files in the log/ directory of your project. So if you open up that folder you'll see a development. log and test.
You could do that with this code
logfile = File.open('/path/to/log.log', 'a')
StatusLogger = Logger.new(logfile)
StatusLogger.info 'Hello World!'
And you would most likely configure this in an initializer file, or you could do it in an environment file if you wanted.
You mean, like having in application.rb:
StatusLogger = ActiveSupport::BufferedLogger.new(Rails.root.join('log/status.log'))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With