2

I'm using grape api. I also use graph logger library for logging all requests from client. I design a general class, because it will be used from many other places.

require 'grape_logging'
class GeneralGrapeApi < Grape::API
  logger.formatter = GrapeLogging::Formatters::Default.new
  use GrapeLogging::Middleware::RequestLogger,
    logger: logger,
    include: [ GrapeLogging::Loggers::Response.new,
               GrapeLogging::Loggers::FilterParameters.new,
               GrapeLogging::Loggers::ClientEnv.new,
               GrapeLogging::Loggers::RequestHeaders.new ]
end

And here is my custom api that extends from above class:

module POS
      class ApplicationApi < GeneralGrapeApi    
        # mounting here
      end
end

When I run, log library doesn't work. It means it doesn't print any log to standard output. But if I move code to inside class, for example:

module POS
  class ApplicationApi < GeneralGrapeApi  
      logger.formatter = GrapeLogging::Formatters::Default.new
      use GrapeLogging::Middleware::RequestLogger,
        logger: logger,
        include: [ GrapeLogging::Loggers::Response.new,
                   GrapeLogging::Loggers::FilterParameters.new,
                   GrapeLogging::Loggers::ClientEnv.new,
                   GrapeLogging::Loggers::RequestHeaders.new ]  
    # mounting here
  end
end

It will work. Please explain for me differences between 2 usages. I prefer first solution because it will make my code doesn't duplicate. How can I fix that.

Thanks

Trần Kim Dự
  • 5,872
  • 12
  • 55
  • 107

1 Answers1

0

It looks like the logger needs to be configured inside the class where the API classes are mounted. So you can use the first option, but then you need to do the mounting in the first section as well and there would be no need for the POS::ApplicationAPI class.

Eric Parshall
  • 731
  • 9
  • 15