Where does RACK log to?

落花浮王杯 提交于 2020-01-01 01:58:08

问题


I am running a sinatra app through RACK.

To which file does the activity get logged ? Also how can I set the log file path ?


回答1:


It depends. Many developers define their app log file to app/servername.log or just to the current path where the Rack app is loaded.

Yes you can change it's path.

Usually you get a config.ru file with something like:

    log = File.new("sinatra.log", "a+")
    $stdout.reopen(log)
    $stderr.reopen(log)

    # optionally to sync logs while the server is running
    $stderr.sync = true
    $stdout.sync = true

and/or

    configure do
      LOGGER = Logger.new("sinatra.log")
      enable :logging, :dump_errors
      set :raise_errors, true
    end

in this case the log file is located under appdir/sinatra.log. But remember this code can be anywhere in your Rack app, so please seek for "log" in your application directory.

    $ cd projectname
    $ grep -ri 'log' *

have fun and post here your config.ru and/or the mainprojectfile.rb.




回答2:


The line of LOGGER = Logger.new("sinatra.log") in @include's answer above did not work for me.

However, an alternative listed here (along with some helpful explanations) worked for me, tested with ruby 2.5.3 and sinatra 2.0.1.

For additional info, that alternative is based on the structure presented in a Sinatra recipe.




回答3:


The object_id are initially the same but it's better to assign to $stderr. That also leaves you open to return the stream to where it was originally with STDERR:

 $ irb
>> $stderr.object_id == STDERR.object_id
=> true

Same object, for now. Send it elsewhere,

>> $stderr = File.open('/tmp/foo', 'w')
=> #<File:/tmp/foo>
>> $stderr.puts "Uh-oh, foo"
=> nil
>> $stderr.flush    # if you want to verify its output
=> #<File:/tmp/foo>
>> $stderr.object_id == STDERR.object_id
=> false

$stderr and STDERR refer to different objects. STDERR still streams to the terminal here,

>> STDERR.puts "Uh-oh, original STDERR"
Uh-oh, original STDERR
=> nil

Restore $stderr,

>> $stderr = STDERR
=> #<IO:0x106fddb88>
>> $stderr.object_id == STDERR.object_id
=> true

And we're back!



来源:https://stackoverflow.com/questions/2366352/where-does-rack-log-to

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!