Last active
August 29, 2015 13:56
-
-
Save iloveitaly/9252194 to your computer and use it in GitHub Desktop.
Snippets for Better Logging & Error Reporting in NetSuite https://docs.google.com/presentation/d/1Ua5SV_kLQWTMOZKyQ0Tgy2gkJEkXe-ZYKMl_NTgVq-w/edit?usp=sharing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/middleware/disable_assets_logger.rb | |
# https://github.com/rails/rails/issues/2639#issuecomment-6591735 | |
# http://stackoverflow.com/questions/6312448/how-to-disable-logging-of-asset-pipeline-sprockets-messages-in-rails-3-1 | |
class DisableAssetsLogger | |
def initialize(app) | |
@app = app | |
Rails.application.assets.logger = Logger.new('/dev/null') | |
end | |
def call(env) | |
previous_level = Rails.logger.level | |
Rails.logger.level = Logger::ERROR if env['PATH_INFO'].index("/assets/") == 0 | |
@app.call(env) | |
ensure | |
Rails.logger.level = previous_level | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /etc/log_files.yml | |
files: | |
- /data/spree/shared/log/production.log | |
- /data/spree/shared/log/unicorn.stderr.log | |
- /data/spree/shared/log/sunspot-solr-production.log | |
- /data/spree/shared/log/ups.log | |
- /data/spree/shared/log/usps.log | |
- /data/spree/shared/log/delayed_job.log | |
- /data/spree/shared/log/netsuite.log | |
- /var/log/cron.log | |
destination: | |
host: logs.papertrailapp.com | |
port: 44690 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
file { "/etc/logrotate.d/yourdomain_com": | |
ensure => file, | |
owner => root, | |
group => root, | |
content => " | |
/data/spree/current/log/production.log | |
/data/spree/current/log/unicorn.stderr.log | |
/data/spree/current/log/ups.log | |
/data/spree/current/log/usps.log | |
/data/spree/current/log/backup.log | |
{ | |
size=10M | |
missingok | |
dateext | |
copytruncate | |
compress | |
delaycompress | |
notifempty | |
rotate 365 | |
} | |
" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# backup/models/mysql_backup.rb | |
Backup::Model.new(:mysql_backup, 'MySQL + Files') do | |
database MySQL do |db| | |
# To dump all databases, set `db.name = :all` (or leave blank) | |
db.name = "spree" | |
db.username = "spree" | |
db.password = "spree" | |
db.socket = "/var/run/mysqld/mysqld.sock" | |
end | |
# mysql backup | |
store_with S3 do |s3| | |
s3.access_key_id = 'yourid' | |
s3.secret_access_key = 'yourkey' | |
s3.region = 'us-east-1' | |
s3.bucket = 'your-bucket/spree/database' | |
s3.keep = 365 | |
end | |
# uploaded files backup | |
sync_with Cloud::S3 do |s3| | |
s3.access_key_id = "yourid" | |
s3.secret_access_key = "yourkey" | |
s3.bucket = "your-backup" | |
s3.path = "/spree/uploads" | |
s3.mirror = true | |
s3.directories do |directory| | |
directory.add "/data/spree/shared/uploaded-files" | |
directory.add "/data/spree/shared/system" | |
end | |
end | |
compress_with Gzip | |
# https://github.com/meskyanichi/backup/wiki/Notifiers | |
notify_by Mail do |mail| | |
mail.on_success = true | |
mail.on_warning = true | |
mail.on_failure = true | |
mail.from = "from@domain.com" | |
mail.to = "to@domain.com" | |
mail.address = "smtp.sendgrid.net" | |
mail.port = 587 | |
# mail.domain = "smtp.sendgrid.net" | |
mail.user_name = "sendgridusername" | |
mail.password = "sendgridpassword" | |
mail.authentication = "plain" | |
mail.encryption = :starttls | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Spree::Payment.class_eval do | |
def gateway_error(error) | |
if error.is_a? ActiveMerchant::Billing::Response | |
text = error.params['message'] || error.params['response_reason_text'] || error.message | |
elsif error.is_a? ActiveMerchant::ConnectionError | |
text = I18n.t(:unable_to_connect_to_gateway) | |
else | |
text = error.to_s | |
end | |
logger.error(I18n.t(:gateway_error)) | |
logger.error(" #{error.to_yaml}") | |
exc = Spree::Core::GatewayError.new(text) | |
Airbrake.notify exc, :parameters => (error.is_a? ActiveMerchant::Billing::Response)? error.params : {} | |
raise exc | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
YourApplication::Application.configure do | |
config.lograge.enabled = true | |
config.lograge.custom_options = lambda do |event| | |
params = event.payload[:params].reject do |k| | |
['controller', 'action'].include? k | |
end | |
{ "params" => params } | |
end | |
config.middleware.swap( | |
Rails::Rack::Logger, Ciunas::Logger, | |
:silenced => [ | |
# "/noisy/action.json", | |
/d24fvsfc6m7i1u\.cloudfront\.net.*/, | |
/shop\/ProdImages\/.*/, | |
/shop\/images\/.*/ | |
] | |
) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
env :PATH, ENV['PATH'] | |
set :output, "/var/log/cron.log" | |
set :path, '/data/spree/current' | |
every 1.day, :at => '5:20 am' do | |
# TODO this path should be able to be dynamic | |
command "cd /data/spree/current && backup perform -t mysql_backup -c /data/spree/current/backup/config.rb" | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# per amy USPS media mail should not be available if item total above | |
# $100 orders via media mail cannot be insured | |
Spree::Calculator::Usps::MediaMail.class_eval do | |
old_available = instance_method(:available?) | |
define_method :available? do |order| | |
return false if order.item_total > 100.0 | |
old_available.bind(self).(order) | |
end | |
end | |
# when we are testing, we want to see the errors... | |
if Rails.env.test? or Rails.env.development? | |
Spree::OrdersController.class_eval do | |
private | |
def handle_shipping_error(e) | |
raise e | |
end | |
end | |
Spree::CheckoutController.class_eval do | |
private | |
def handle_shipping_error(e) | |
raise e | |
end | |
end | |
else | |
# in production, push all errors to airbrake | |
Spree::OrdersController.class_eval do | |
private | |
def handle_shipping_error(e) | |
Airbrake.notify e | |
flash[:error] = e.message | |
redirect_to checkout_state_path(:address) | |
end | |
end | |
Spree::CheckoutController.class_eval do | |
private | |
def handle_shipping_error(e) | |
Airbrake.notify e | |
flash[:error] = e.message | |
redirect_to checkout_state_path(:address) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment