SSP
SSP

Reputation: 2670

ruby logger file behaviour

When i am using this script its working fine

require 'logger'

class Logging < Logger
    log = Logger.new "sam.txt"
    log.warn "samarth"
end

and output of log file is coming in this way

# Logfile created on Thu Sep 20 12:01:26 +0530 2012 by /
W, [2012-09-20T12:01:44.402915 #7553]  WARN -- : asasa

Now i want to make a class which inherit logger and i am using that script in all other but its producing some different output

require 'date'
require 'logger'

class Logging < Logger

    def initialize 
      @date = Time.now.strftime("20%y-%m-%d").to_s
      @time = Time.now.strftime("%X")
      @file_name = @date+"_"+@time+ " log.txt"
      @file_name_path = '/cmf/samnew/build/Logs/'+@file_name
    end

    def createfile
      $log1 = Logger.new(@file_name_path)
      return $log1 
    end

end

i am implementing this script in below script

require '/cmf/samnew/build/Controller/logging'


class Controller

    #Generating log file

    log = Logging.new
    log1 =  log.createfile
    log1.warn "samarth global"

end

and output is like

# Logfile created on Thu Sep 20 12:03:20 +0530 2012 by /
samarth global

So what is wrong in this code or what should i do to create output like above

Upvotes: 2

Views: 122

Answers (1)

peter
peter

Reputation: 42182

Probably one of the gems you load messes up the global formatting string.

Try this to define your wished format

def createfile 
  $log1 = Logger.new(@file_name_path) 
  $log1.formatter = proc do |severity, datetime, progname, msg| 
    "#{severity[0]}, [#{datetime} \##{$$}] #{severity} --> #{msg} \n"
  end
  $log1  
end 

Upvotes: 1

Related Questions