Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8018281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:05:46+00:00 2026-06-04T21:05:46+00:00

I’m attempting to setup logging in my simple classic-style sinatra app. (v1.3.2) I define

  • 0

I’m attempting to setup logging in my simple classic-style sinatra app. (v1.3.2) I define all of my setup in an environment.rb file like this:

require 'sinatra'
require 'couchrest'
require 'couchrest_model'
require 'json'
require "net/http"
require "uri"
require 'shotgun'  # to auto-restart server on page reload

enable :run, :logging

# CouchDB setup
configure do
  SiteConfig = OpenStruct.new(
                              :url_base => 'http://localhost:4567/',
                              :url_base_db => 'http://localhost:5984/',
                              :db_name => 'adrequest'
                              )
end

# Logging setup
logger = Logger.new('vehicle.log')
use Rack::CommonLogger, logger

My app file looks like this:

require './environment'

class Vehicle < CouchRest::Model::Base
  use_database CouchRest.database!((SiteConfig.url_base_db || '') + SiteConfig.db_name)

  property :url, String
  timestamps!

  validates_presence_of :url
end

get '/*' do

  requesturl = params[:splat].first

  @vehicle = Vehicle.new
  @vehicle.url = requesturl
  @vehicle.save
  puts "logger: #{env['rack.logger']}"
  puts "env: #{ENV['RACK_ENV']}"
  logger.info "written to DB"
end

not_found do
  halt 404, 'page not found'
end

Results in:

logger: #<Rack::NullLogger:0x007fb4f4ccc238>
env: development

The app creates a the ‘vehicle.log’ file, but it’s empty. How do I get logger.info to write to my ‘vehicle.log’ file?

I’ve read the Sinatra README here: http://www.sinatrarb.com/intro#Logging. It seems to imply that all that I need is the:

enable :logging

and then sinatra will take the rest of the configuration from Rack::CommonLogger, but it doesn’t say how to do that exactly…

Thanks in advance for any ideas!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T21:05:47+00:00Added an answer on June 4, 2026 at 9:05 pm

    The reason that “logger” never gets enabled in the app file is because it has local scope in the environment.rb file. In general, a Ruby variable that is defined in a file is scoped locally within that file.

    My solution for this was to make the “logger” variable global:

    $logger = Logger.new('vehicle.log')
    

    While usage of global variables are generally frowned upon, this app is simple enough that it’s probably ok. Nevertheless, if the app were to get larger it would probably make sense to look at a more module-based app and handle configuration in a different way. (Maybe with Modules, or configu.ru file?)

    I’m not sure if this is the “ideal” solution, so if anyone has any better ideas please comment.

    Incidentally, the SiteConfig variable defined in the environment.rb file turns out to be a constant (you can see this by doing puts "SiteConfig: #{defined? SiteConfig}"), which is why it’s value can be “seen” in the separate app file.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.