Skip to content
This repository has been archived by the owner on May 20, 2021. It is now read-only.

feat: Change logs to JSON format #83

Merged
merged 5 commits into from
Jul 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,5 @@ end
gem "dogstatsd-ruby", "~> 4.8"

gem "ddtrace", "~> 0.38.0"

gem "lograge", "~> 0.11.2"
8 changes: 8 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ GEM
listen (3.2.1)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
lograge (0.11.2)
actionpack (>= 4)
activesupport (>= 4)
railties (>= 4)
request_store (~> 1.0)
loofah (2.4.0)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
Expand Down Expand Up @@ -185,6 +190,8 @@ GEM
rb-fsevent (0.10.3)
rb-inotify (0.10.1)
ffi (~> 1.0)
request_store (1.5.0)
rack (>= 1.4)
responders (2.4.1)
actionpack (>= 4.2.0, < 6.0)
railties (>= 4.2.0, < 6.0)
Expand Down Expand Up @@ -273,6 +280,7 @@ DEPENDENCIES
haml-rails
jwt
listen
lograge (~> 0.11.2)
mysql2 (>= 0.3.13, < 0.5)
puma
rack-cors
Expand Down
19 changes: 19 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,28 @@ class ApplicationController < ActionController::Base
respond_to :html, :json
layout :false

def route_not_found
render 'error_pages/404', status: :not_found
end

protected

def set_csrf_cookie
cookies['XSRF-TOKEN'] = form_authenticity_token if protect_against_forgery?
end

def append_info_to_payload(payload)
super

unless payload[:status]
return
end

payload[:level] = 'INFO'
if payload[:status] >= 500
payload[:level] = 'ERROR'
elsif payload[:status] >= 400
payload[:level] = 'WARN'
end
end
end
14 changes: 14 additions & 0 deletions app/views/error_pages/404.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> Standard File Server </title>
<link rel="stylesheet" href="assets/home.css"/>
</head>

<body>
<h1> Hi! the page you are looking for could not be found. </h1>

<p> You might be looking for the <a href="https://app.standardnotes.org"> Standard Notes Web App</a> or the main <a href="https://standardnotes.org"> Standard Notes Website</a>. </p>
</body>
</html>
6 changes: 3 additions & 3 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@

config.logger = ActiveSupport::Logger.new(config.paths['log'].first, 1, 1 * 1024 * 1024)

require 'custom_log_formatter'
config.log_formatter = CustomLogFormatter.new
config.logger.formatter = config.log_formatter
if ENV["RAILS_LOG_TO_STDOUT"].present?
config.logger = ActiveSupport::Logger.new(STDOUT)
end

# Do not eager load code on boot.
config.eager_load = false
Expand Down
11 changes: 1 addition & 10 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,8 @@
MAX_LOG_MEGABYTES = 50
config.logger = ActiveSupport::Logger.new(config.paths['log'].first, 1, MAX_LOG_MEGABYTES * 1024 * 1024)

require 'custom_log_formatter'
config.log_formatter = CustomLogFormatter.new
config.logger.formatter = config.log_formatter

if ENV["RAILS_LOG_TO_STDOUT"].present?
logger = ActiveSupport::Logger.new(STDOUT)
logger.formatter = config.log_formatter
config.logger = ActiveSupport::TaggedLogging.new(logger)
config.logger = ActiveSupport::Logger.new(STDOUT)
end

# Eager load code on boot. This eager loads most of Rails and
Expand Down Expand Up @@ -50,7 +44,4 @@

# Send deprecation notices to registered listeners.
config.active_support.deprecation = :notify

# Use default logging formatter so that PID and timestamp are not suppressed.
config.log_formatter = ::Logger::Formatter.new
end
4 changes: 3 additions & 1 deletion config/environments/raspberry_pi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
config.reload_classes_only_on_change = true

config.logger = ActiveSupport::Logger.new(config.paths['log'].first, 1, 1 * 1024 * 1024)
config.logger = Logger.new(STDOUT)
if ENV["RAILS_LOG_TO_STDOUT"].present?
config.logger = Logger.new(STDOUT)
end
config.logger.level = Logger::INFO

# Do not eager load code on boot.
Expand Down
11 changes: 1 addition & 10 deletions config/environments/staging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,8 @@
# Code is not reloaded between requests.
config.cache_classes = true

# 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)
config.logger = ActiveSupport::Logger.new(STDOUT)
end

# Eager load code on boot. This eager loads most of Rails and
Expand Down Expand Up @@ -67,9 +61,6 @@
# Send deprecation notices to registered listeners.
config.active_support.deprecation = :notify

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

# Do not dump schema after migrations.
# config.active_record.dump_schema_after_migration = false
end
14 changes: 14 additions & 0 deletions config/initializers/lograge.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Rails.application.configure do
config.lograge.enabled = true

# Generate log in JSON
config.lograge.formatter = Lograge::Formatters::Json.new
config.lograge.custom_options = lambda do |event|
{
:ddsource => ["ruby"],
:time => event.time,
:params => event.payload[:params],
:level => event.payload[:level]
}
end
end
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,7 @@
post "items/sync" => "items#sync"
end

get '*unmatched_route', to: 'application#route_not_found'

root "application#home"
end
1 change: 1 addition & 0 deletions env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# Rails Settings
EXPOSED_PORT=3000
RAILS_ENV=development
RAILS_LOG_TO_STDOUT=false
#SQS_QUEUE=somequeue
#AWS_REGION=us-west1

Expand Down
29 changes: 0 additions & 29 deletions lib/custom_log_formatter.rb

This file was deleted.

1 change: 0 additions & 1 deletion spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
SimpleCov.start :rails do
add_filter %r{^/spec/}
add_group 'Sync Engine', 'lib/sync_engine'
add_filter %r{/lib/custom_log_formatter.rb}
add_filter %r{/app/mailers}

# Remove 'Libraries' tab
Expand Down