1

I'm making use of log rotation within a Rails app, which seems to function. However, the new log files are owned by the root user rather than the apps user. The application is running via Passenger which is using the apps user too.

Is it possible to have the new logs created by / owned by the apps user instead of root?

The config I'm using in production.rb is below:

# Use default logging formatter so that PID and timestamp are not suppressed.
config.log_formatter = ::Logger::Formatter.new

config.logger = Logger.new(config.paths["log"].first, 3, 10.megabytes)

# Use a different logger for distributed setups.
# require 'syslog/logger'
# config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name')

if ENV["RAILS_LOG_TO_STDOUT"].present?
  logger           = ActiveSupport::Logger.new(STDOUT)
  logger.formatter = config.log_formatter
  config.logger = ActiveSupport::TaggedLogging.new(logger)
end
Luke Smith
  • 682
  • 6
  • 18
  • Are you sure you are running rails as the correct user? You can add something like `u = %x(whoami).strip; raise "Wrong user. Current user is #{u}" unless u == "apps"` to test it. This assumes that the system supports [`whoami`](http://man7.org/linux/man-pages/man1/whoami.1.html). – max Jan 19 '18 at 10:39

1 Answers1

0

I had exactly the same problem. Turns out I had a copy of the process running as a service (using systemctl) - I thought I had disabled the service temporarily. In this case the log file would be owned by 'root'.

When I started the second version in the foreground for testing, as the current user, everything appeared to work fine, but eventually it would try to rotate the log file that belonged to root, which would fail, effectively disabling logging.

djm
  • 69
  • 6
  • Hi, sorry for the hit up on an old question, but I have the exact same problem. What background process are you referring to? I suspect it is the same problem I'm having, just not sure what ActiveSupport::Logger uses and what to look for in services. – Stewart McEwen Sep 14 '20 at 22:57
  • The background process is my Rails web server that uses ActiveSupport. It is controlled by systemctl, which starts and restarts it automatically using a designated user (probably root, at that time). This would set ownership of the log files to root. Later, when I started the server manually the user would be set to ubuntu, which didn't have the permissions to rotate the (root) log files, leading to the failure. – djm Aug 31 '21 at 19:27