Add logs in different directory using daemon in ruby

陌路散爱 提交于 2019-12-13 05:23:08

问题


I am using daemon to wrap my script and has specified logs location into that : Script looks like this :

#!/usr/local/bin/ruby
require 'rubygems'
require 'daemons'

Daemons.run_proc(
   'script_test', # name of daemon
   :log_output => true,
   :output_logfilename => "script-test.log",
   :logfilename => "script-test.log"
 ) do
   exec 'ruby /opt/script-test/script-test.rb'
end

Problem is my logs are storing in same directory where my script is present. I have to add my logs to different directory such as /var/log/script-test and later have to rotate those logs weekly. Provide me with a solution so that i can store the logs of script in /var/log directory.


回答1:


Make sure you are using an absolute path instead of a relative path

For example:

:output_logfilename => "/var/log/script-test.log",
:logfilename => "/var/log/script-test.log"

In order to logrotate your logs, (assuming Linux) add the following to your logrotate config to rotate on a weekly basis:

/var/log/script-test.log {
  weekly
  missingok
  compress
  notifempty
  copytruncate
}



回答2:


It worked for me with this configuration as :

Daemons.run_proc(
   'script-test', # name of daemon
   :log_output => true,
   :dir_mode => :normal,
   :dir => "/var/log",
   :output_logfilename => "script-test.log",
   :logfilename => "script-test.log"
 ) do
   exec 'ruby /opt/script-test/script-test.rb'

end



来源:https://stackoverflow.com/questions/34714495/add-logs-in-different-directory-using-daemon-in-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!