Marshal::dump and instance of logger

M

Minkoo Seo

Hi group.

I got a question on Marshal::dump and Logger. I've been used @logger
which contains Logger.new to log message to STDOUT like:

class Foo
def initialize
@logger = Logger.new(STDOUT)
end

def bar
@logger.info(...)
end
end

Unfortunately, Marshal::dump does not allow me to add such instance
variables and raises an error if such one exists.

That being the case, how can I add logger instance to a class w/o
hampering marshalling? Is there any design pattern that I can use?

Sincerely,
Minkoo Seo
 
T

ts

M> Unfortunately, Marshal::dump does not allow me to add such instance
M> variables and raises an error if such one exists.

Use #marshal_dump, #marshal_load

moulon% cat b.rb
#!/usr/bin/ruby
require 'logger'
class A
attr_reader :a, :logger

def initialize
@a = 12
@logger = Logger.new(STDOUT)
end

def bar
@logger.info("info")
end

def marshal_dump
@a
end

def marshal_load(x)
@a = x
@logger = Logger.new(STDOUT)
end
end

a = A.new
b = Marshal.load(Marshal.dump(a))
p b.a, b.logger.class
moulon%

moulon% ./b.rb
12
Logger
moulon%
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top