How to create a folder (if not present) with Logger.new?
Try something like this.
dir = File.dirname("#{Rails.root}/log/#{today}/my.log")
FileUtils.mkdir_p(dir) unless File.directory?(dir)
@@my_logger ||= Logger.new("#{Rails.root}/log/#{today}/my.log")
Python logging: create log if not exists or open and continue logging if it does
The logging module's FileHandler
takes care of that for you. No need for complexity.
The handler takes an optional mode
parameter, to specify whether it starts writing or appending data to it.
From the docs:
class logging.FileHandler(filename, mode='a', encoding=None, delay=False)
The specified file is opened and used as the stream for logging. If
mode
is not specified,'a'
is used.
How to create a log folder before rails application loads the config.logger
Copy Paste it config/environments/development.rb
log_file_name = "#{Rails.root}/logs/#{ENV['RAILS_ENV']}.log"
unless File.exist?(File.dirname(log_file_name))
FileUtils.mkdir_p(File.dirname(log_file_name))
File.new(log_file_name, 'a+')
end
config.logger = ActiveSupport::BufferedLogger.new(File.join("#{Rails.root}", "logs", "#{ENV['RAILS_ENV']}.log"))
For 'a+' in File.new see this LINK
You can also use the config/application.rb LINK
Only create logs' directory if logger has file handler
The dictConfig()
documentation shows how you can configure handlers of a particular type, and that can be extended to your own handlers.
You could subclass FileHandler
and override the emit()
method like this:
def emit(self, record):
dn = os.path.dirname(self.baseFilename)
if not os.path.exists(dn):
os.makedirs(dn)
super().emit(record)
and then use this subclass in your logging configuration. However, make sure you instantiate the handler with delay=True
and, if you override __init__()
in your custom handler, call the parent class' __init__()
from there. The delay=True
doesn't try to open the file until there is an actual need to write to it; that's done by the FileHandler.emit()
method, and by creating the directory in the overridden method before calling the base class' emit()
method, you'll only create the directory when it's needed and doesn't already exist.
How to create directories for Logger files through FileHandler
The j.u.l.FileHandler can't create directories. According to the API spec, nonexistent directories are and or should be treated as invalid. Which means your logs should appear in the user home directory instead. This described in JDK-6244047: impossible to specify directories to logging FileHandler unless they exist:
Configuration: By default each FileHandler is initialized using the
following LogManager configuration properties. If properties are not
defined (or have invalid values) then the specified default values
are used.
- java.util.logging.FileHandler.level specifies the default level
for the Handler (defaults to Level.ALL).
<snip>
- java.util.logging.FileHandler.pattern specifies a pattern for
generating the output file name. See below for details.
(Defaults to "%h/java%u.log").
Based on the spec wording above, if the "FileHandler.pattern" property
specifies an unusable value, then it is invalid. If an invalid value
is specified, then the API is supposed to use the default value. In
this case "%h/java%u.log" should be used.
If you need to create directories then you can use the LogManager config option or subclass the FileHandler.
See also: JDK-6258319: No exception with FileHandler file has %h, but %h does not exist
Related Topics
How to Completely Remove Webpack and All Its Dependencies from Rails App
Ruby on Rails HTML-Table Generator
How to Run Ruby in Haml in JavaScript Definition
Running a Shell Command from Ruby: Capturing the Output While Displaying the Output
Ruby Rake Load Tasks from a Gem
Jekyll Templates Using Django-Like Liquid Blocks/Inheritance
How to Use Coffeescript in the Views Executed on Render.Js
How to Get Rufus-Scheduler Working with a Rails App Deployed to Heroku
Getting All Links of a Webpage Using Ruby
Set Compression Level When Generating a Zip File Using Rubyzip
How to Calculate Next, Previous Business Day in Rails
Error Connecting to Redis on 127.0.0.1:6379 (Errno::Econnrefused) - Wercker
Solving Dependency Constraints