Rails logging to Apache logs rather than application log - ruby-on-rails

I am running a Rails application on Apache using mod_passenger. I would like Rails.logger calls to write to the Apache error log rather than to the application's log file in log/production.log.
How can I do this?

In your config/environments/production.rb file you can add something like:
config.logger = Logger.new("/var/log/apache2/error.log")
Of course your app will need to have permissions to such a file. In addition intermixing Apache errors with your apps logs is definitely not a good idea.

This doesn't answer your question directly but I've just run a little test and STDERR.puts "meep" ended up in Apache's error log while using mod_passenger.
Perhaps then you could point config.logger at STDERR?

Related

Separate logging for rails application in tomcat

We have three apps running in tomcat at different context path. I am using the default context settings in context.xml. We would like one one of the applications that logs to STDOUT to be able to log to its own file.
It is a ruby on rails application that creates the logger as below. It is built as war using jruby file and deployed to tomcat.
if ENV["LOG_STDOUT"].present?
logger = ActiveSupport::Logger.new(STDOUT)
logger.formatter = config.log_formatter
config.logger = ActiveSupport::TaggedLogging.new(logger)
end
We would like to redirect the output from the app to go to it's own file. At the moment, all output from the rails application goes to localhost.date.txt file. Other apps also log to the same file. For instance, a grails app logs unhandled exceptions to the same file.
Questions:
What is the easiest way to redirect the log output to STDOUT from an app at context path /rails/search it's own file without changing the application code?
I have tried with log4j.jar being copied to the appropriate folders if they exist or create the folders and copy of they dont as per the docmentation on tomcat website without much luck.
How can I stop the app at context /grails/hello to stop logging to the localhost.date.txt file.
I have disabled root logging to STDOUT in grails app log4j config, but it still logs unhandled exceptions. In its log file I have restricted the stacktrace to 5 lines, but in localhost.date.txt file it still prints the full stacktrace. I can disable all logging to consoleHandler, but I cannot do it unless I solve the first question. So any help is appreciated.
Things Tried:
Use log4j and slf4j to redirect logs to their own file using swallowOutput on context
Create new tomcat9 handler, but could'nt marry it to the logging class for rails app
Added properties files at appName/WEB_INF/classes/logging.properties and the file is created but no output is logged to the file.

Rails logs in staging are only writing to my log files. How do I get them to log to STDout or papertrail?

I currently have this issue that when there's an error, the logs in my terminal aren't informative. The logs are only writing to the log files but my terminal output doesn't show anything. This was super confusing. How do I change this?
What is the Rails default behavior anyway when it comes to logging?
In Rails, 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.
But if you want to change you can specify something like below in config/application.rb which would throw out logs on STDOUT.
config.logger = Logger.new(STDOUT)
See the Rails guides for more info.
What I usually found easy was to tail the log on console like $ tail -f log/development.log and force/see the output of log file on console.

Jruby on Rails logging with Tomcat

I have a jruby/rails (4.1) app that I want to deploy through Tomcat. I made the app log through stdout by adding
config.logger = Logger.new(STDOUT)
in config/application.rb, and the log lines are going to catalina.log.
We would like the app logs to go to a separate Tomcat logfile. Is it possible? how can it be configured?
Thanks!
if you do not set config.logger at all ... it will log to where servlet log goes
so simply do not do anything at all with config.logger (just leave the default) and it should work (assuming JRuby-Rack of course). otherwise there are 2 options :
use a Java logging library (in case of TC that is either JUL or Log4J) and configure JRuby-Rack to delegate
or simply config.logger = Logger.new('logs/rails.log') in this case make sure you set a formatter to include timestamps
NOTE: be aware that in the first case the Java logging library will receive all logs from Rails at the info level (loggers are not mapped "1-to-1") ... this might change in a future JRuby-Rack 1.2 release.

Phusion Passenger doesn't write log files

I have installed phussion passenger on osx following this guide from apple. I also used the PassengerPane to configure it.
It works, but it doesn't write anything to development.log. It's not a permissions problem.
Do you have any idea why?
OK, I found it.
Logging wasn't working because of these lines in application.rb:
config.logger = Logger.new(STDOUT)
config.log_level = :info
These were required for using Heroku's logging, but not anymore.
Passenger runs on production by default. Check production.log, you should see content there.

My Rails app is returning HTTP 500 for all its URLs, but nothing shows up in the log file. How can I diagnose the problem?

I have a Rails app that is running on a production server with Apache and Phusion Passenger. The app works fine locally when using Mongrel, but whenever I try to load a URL on the production server, it returns HTTP 500. I know the server is working properly, because I can get the static elements of the application (e.g., JavaScript files, stylesheets, images) just fine. I've also checked the Passenger status and it is loading the app (it must be, since the app's 500 Internal Server Error page is returned, not just the default Apache one). Also, when I load the app via script/console production and do something like app.get("/"), 500 is also returned.
The problem is that there is nothing in the log files to indicate the problem. production.log is empty. The Apache error logs show no problems with Apache, either. I'm stumped as to what's going on and I'm not sure how to diagnose the problem.
I know I may have been a bit vague, but can anyone give a suggestion on what the problem may be? Or at least a way I can go about diagnosing it?
The answer for this specific situation was a problem with my app. One of the model classes used a different database connection than the rest of the app. This database connection was not configured properly. I think the reason why nothing was written to the log files is that Rails bailed out without having any idea what to do.
Since it may be helpful for others to see how I diagnosed this problem, here was my thought process:
The issue couldn't be with Apache: no errors were written into the Apache log files.
The issue probably wasn't with Passenger: Passenger wasn't writing any errors to the Apache log file, and it seemed to be loading my app properly, since passenger-status showed it as loaded and it was display my app's 500 Internal Server Error page (not the default Apache one).
From there I surmised that it must be something broken in my app that happened very early on in the initialization phase, but wasn't something that caused the app to completely bail and throw an exception. I poked around in the Phusion Passenger Google Group, and ultimately stumbled upon this helpful post, which suggested that the error may be a database connectivity issue. Sure enough, removing this misconfigured database and all references to it made the app work!
Have you tried running the app locally using Passenger?
Try running the application locally on Mongrel in Production mode, to make sure that there's no weird issues with that particular environment. If that works, then you know that it's not an issue with your codebase. Since your static components are being served properly, that tells me that Apache is working fine. The only gear in the system left is Passenger. At this point, I would say it's an improperly configured Passenger. You should post up your Passenger config file, and ask the question on ServerFault.
A couple of things to try :
Have you gone though the following from the docs:
6.3.7. My Rails application’s log file is not being written to
There are a couple things that you
should be aware of:
By default, Phusion Passenger runs Rails applications in production
mode, so please be sure to check
production.log instead of
development.log. See RailsEnv for
configuration.
*
By default, Phusion Passenger runs Rails applications as the owner
of environment.rb. So the log file can
only be written to if that user has
write permission to the log file.
Please chmod or chown your log file
accordingly.
See User switching (security) for details.
If you’re using a RedHat-derived Linux
distribution (such as Fedora or
CentOS) then it is possible that
SELinux is interfering. RedHat’s
SELinux policy only allows Apache to
read/write directories that have the
httpd_sys_content_t security context.
Please run the following command to
give your Rails application folder
that context:
Have you checked your vhost or httpf.conf file ? Do you have any logging directives ?
Check the top level apache log file
Try setting PassengerLogLevel to 1 or 2 or 3, as shown here http://www.modrails.com/documentation/Users%20guide.html#_passengerloglevel_lt_integer_gt
Do you have any rack apps installed ?
My suggestion would be to go right back to "Hello World" land and create the smallest possible Ruby example application and upload it to see if there is a problem with Passenger or Ruby on the server.
May be a silly suggestion but I suggest you start by increasing the logging levels on production while you are testing. Do this in config/environments/production.rb and use:
config.log_level = :debug
This should at least get you some sort of backtrace so you can start to find the problem.
If you still get nothing - you may find that you have an issue with something as simple as a missing gem/plugin on your production server. That sort of thing may well manifest as a "500" error and just not be very verbose for you.
Can you run the test suite on your production server?

Resources