Support Logging setup

                    Rails Logging

                    Rails Logging

                    You can send your Rails logs to Loggly over syslog. We’re using the lograge gem because it offers better formatting of logs, but it’s not required. We will automatically parse JSON and specific types of rails logs. For alternatives, please see the Advanced Options section below.

                    Rails Logging Setup

                    1. Configure syslog daemon

                    Run our automatic configure-linux script below to setup Rails logging and send the logs to Loggly through your syslog daemon.

                    curl -O http://www.qianj.site/install/configure-linux.sh
                    sudo bash configure-linux.sh -a SUBDOMAIN -u USERNAME
                    

                    Replace:

                    • SUBDOMAIN: your account subdomain that you created when you signed up for Loggly
                    • USERNAME: your Loggly username

                    2. Configure rsyslog to send Rails app logs to Loggly

                    Open your ruby configuration file for rsyslog or create a new one:

                    sudo vim /etc/rsyslog.d/21-rails.conf
                    

                    Paste this configuration into the file then save it.

                    #provides UDP syslog reception
                    $ModLoad imudp
                    $UDPServerRun 514
                    
                    #Add a tag for Rails events
                    $template LogglyFormatRails,"<%pri%>%protocol-version% %timestamp:::date-rfc3339% %HOSTNAME% %app-name% %procid% %msgid% [TOKEN@41058 tag=\"Rails\"] %msg%\n"
                     
                    local7.* @@logs-01.loggly.com:514;LogglyFormatRails
                    local7.* ~ #discards the messages so they don't end up in /var/log/syslog
                    

                    Replace:

                    Then restart rsyslog so the changes take effect

                    sudo service rsyslog restart
                    

                    3. Configure Rails application to send Logs

                    Open Gemfile in your application. It should be present in the root folder of your application.

                    vim Gemfile

                    Add the following statements to the Gemfile to include the syslogger and lograge gems in your application

                    gem 'syslogger', '~> 1.6.0'
                    gem 'lograge','~> 0.3.1'
                    

                    Install these gems in RVM using the following command

                    bundle install
                    

                    Add the following code in your <environment>.rb config file. You can find it in the config/environments folder present in your application directory structure. You can select .rb file on the basis of your current environment setup. It can be development.rb, test.rb or production.rb

                    require 'syslogger'
                    config.logger = Syslogger.new("APPNAME", Syslog::LOG_PID, Syslog::LOG_LOCAL7)
                    config.lograge.enabled = true
                    config.lograge.formatter = Lograge::Formatters::Json.new
                    
                    • APPNAME: your application name

                    4. Send a Test Event

                    Now use the logger in your application.

                    logger.info "Hello from Rails!"
                    

                    Restart your rails application and visit a page so that it starts sending events to Loggly.

                    5. Verify Events

                    Search Loggly for events with the Rails tag over the past 20 minutes. It may take a few minutes to index the event. If it doesn’t work, see the troubleshooting section below.

                    tag:Rails
                    
                    Rails Logging Example

                    Click on one of the logs to show a list of Rails fields (see screenshot below). If you don’t see them, please check that you are using one of our automatically parsed formats.

                    Advanced Rails Logging Options

                    Troubleshooting Rails Logs

                    If you don’t see any data show up in the verification step, check for these common problems.

                    Check syslogger configuration:

                    • If the “bundle install” command fails to execute, check if you are running bundle version 1.11.2 or any older version of bundle. Updating bundle version to 1.16.3 works perfectly. Also, always install Ruby using RVM. This will automatically install/update the bundle version to updated one.
                    • Wait a few minutes in case indexing needs to catch up.
                    • Check to see if the 01-ruby-logger.conf is properly created and placed in the proper location. It should be in the /etc/rsyslog.d folder.
                    • Check that UDP reception is enabled on port 514 in /etc/rsyslog.conf file.
                    • See our HTTP Troubleshooting Guide to verify HTTP events are being sent to Loggly.
                    • See our Rsyslog Troubleshooting Guide if the files are not being sent to Loggly.

                    Still Not Working?

                    Thanks for the feedback! We'll use it to improve our support documentation.


                    青青操