Facing issue with Python loggin logger for printing object value

Dave Angel d at davea.name
Fri Dec 28 16:39:56 CET 2012


On 12/28/2012 09:27 AM, Morten Engvoldsen wrote:
> Hi Team,
> i am new to python

Welcome.

>  and i am using python loggin for log the value of the
> object. Below is my code :
>
> class field:
>     field_name = ""
>     length = 0
>     type = 0
>     value = ""
>
>     def __init__(self, field_name, length, type, value):
>         self.field_name = field_name
>         self.length = length
>         self.type = type
>         self.value = value
>
>     def toString(self):
>         if self.type == 2:
>             return self.value.zfill(self.length)
>         else:
>             return self.value.ljust(self.length).upper()
> class record:
>     fields = []
>
>     def setValue(self, field_name, value):
>         for element in self.fields:
>             if field_name == element.field_name:
>                 element.value = value
>
>     def toString(self):
>         _tempStr = ""
>         for element in self.fields:
>             _tempStr = _tempStr + element.toString()
>         if len(_tempStr) < 80:
>             return _tempStr
>         else:
>             _lines = len(_tempStr) / 80
>             _i = 0
>             _tempStr2 = ""
>             _newline = ""
>             while _i < _lines:
>                 _tempStr2 = _tempStr2 + _newline + _tempStr[_i*80:(_i+1)*80]
>                 _newline = "\n"
>                 _i = _i + 1
>
>             return _tempStr2
> class First_betfor00(record):
>
>     def __init__(self):
>         self.fields = [field("APPLICATION-HEADER", 40, 1, ""),
>               field("TRANSACTION-CODE", 8, 0, "BETFOR00"),
>               field("ENTERPRISE-NUMBER", 11, 2, ""),
>               field("DIVISION", 11, 1, ""),
>               field("SEQUENCE-CONTROL", 4, 2, ""),
>               field("RESERVED-1", 6, 1, ""),
>               field("PRODUCTION-DATE", 4, 1, "MMDD"),
>               field("PASSWORD", 10, 1, ""),
>               field("VERSION", 10, 1, "VERSJON002"),
>               field("NEW-PASSWORD", 10, 1, ""),
>               field("OPERATOR-NO", 11, 1, ""),
>               field("SIGILL-SEAL-USE", 1, 1, ""),
>               field("SIGILL-SEAL-DATE", 6, 1, ""),
>               field("SIGILL-SEAL-KEY", 20, 1, ""),
>               field("SIGILL-SEAL-HOW", 1, 1, ""),
>               field("RESERVED-2", 143, 1, ""),
>               field("OWN-REFERENCE-BATCH", 15, 1, ""),
>               field("RESERVED-3", 9, 1, ""),
>               ]
> class account(osv.osv_memory):
>  _name = 'account'
>
>  def create(self,cr,uid,ids,context):
>          logger = logging.getLogger('account')
>          hdlr = logging.FileHandler('/var/tmp/account')
>          formatter = logging.Formatter('%(asctime)s, %(levelname)s,
> %(message)s')
>          hdlr.setFormatter(formatter)
>          logger.addHandler(hdlr)
>
>          batch = ""
>          recordCounter = 1
>          dateMMDD = time.strftime('%m%d')
>
>          betfor00 = Format_betfor00()
>          betfor00.setValue("APPLICATION-HEADER",
> applicationHeader.toString())
>          betfor00.setValue("ENTERPRISE-NUMBER", enterpriseNumber)
>          betfor00.setValue("PRODUCTION-DATE", dateMMDD)
>          batch = betfor00.toString()
>
>             line_counter = line_counter + 1
>             log.debug('%(batch)s')
>         return {'type': 'state', 'state':'end'}
> account()

Is this code supposed to represent the whole program, or are you
"simplifying" for us by leaving stuff out ?  Or worse, are you retyping
all or parts of it ?  Please use cut & paste to create the message, tell
us the exact environment you're using, and how you tried running the script.

When I try to run this on Python 2.7, the first thing I get is
"unexpected indent".   After fixing that, I get NameError: name 'osv' is
not defined.  After kludging something up for that, I get the same
result you describe.  The program does nothing.


This code has nearly the same problem as your different code of 4 days
ago.  You have only one line of top-level code, and it does nothing
useful.  So of those four classes, only one gets instantiated.  And
since 'account' has no __init__() method (nor any __new__() method),
nothing happens during instantiation.  And you toss out the new account
object and exit the program.

Perhaps you meant to save the account class instance with some name. 
And perhaps you meant to then call create() on it with 4 explicit
arguments.  But after I make some changes like that, I get the error: 
NameError: global name 'logging' is not defined.  Then when I fix that,
I see NameError: global name 'time' is not defined.  Then when i fix
that, I see NameError: global name 'Format_betfor00' is not defined. 
When i change that to First_betfor00, I see NameError: global name
'applicationHeader' is not defined.

By this time, my file has diverged significantly from yours.  I can see
more typos in the lines ahead (log instead of logger, for one), but I
don't have the patience.

Just where did this "program" come from?  And how much experience do you
have programming in any language, and how much in Python?  If you're new
to both, I'd suggest you start with a tutorial, and don't try creating
classes and methods till you have the basics of the language well
understood.

Try the page http://www.python.org/about/gettingstarted/   And be
careful to pick a tutorial that matches the version of Python (2.x or
3.x) that you're actually using.  Or vice versa.


-- 

DaveA




More information about the Python-list mailing list