Skip to content

Commit

Permalink
Use dict literal instead of constructor where possible and add some p…
Browse files Browse the repository at this point in the history
…erf related docs
  • Loading branch information
nir0s committed Feb 24, 2018
1 parent 1ca6fa1 commit 1646a41
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,13 @@ wryter.unbind('cid')

The idea behind this is that a cid can be passed into any log message within the same context. "within the same context" is a very abstract defintion, and is up to the developer to implement as it might be thread-related, framework-related, or else. I intend to expand the framework, but for now, that's what it is.


## Performance optimizations

* Using dict literal instead of constructor (https://stackoverflow.com/a/6612024/3332312)
* Using tuples instead of lists (https://stackoverflow.com/a/68817/3332312)
* Using dict subscription instead of get (https://stackoverflow.com/questions/7631929/python-dictionary-datastructure-which-method-d-or-d-get#comment58682247_7631951)

## Testing

```shell
Expand Down
20 changes: 11 additions & 9 deletions wryte.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,11 +260,12 @@ def _get_base(name, hostname):
"""Generate base fields for each log message.
"""
# TODO: Document that these are only generated once.
return dict(
name=name,
hostname=hostname or socket.gethostname(),
pid=os.getpid(),
type='log')
return {
'name': name,
'hostname': hostname or socket.gethostname(),
'pid': os.getpid(),
'type': 'log'
}

@staticmethod
def _logger(name):
Expand Down Expand Up @@ -340,10 +341,11 @@ def _enrich(self, message, level, objects, kwargs=None):
if kwargs:
log.update(kwargs)

log.update(dict(
message=message,
level=level.upper(),
timestamp=self._get_timestamp()))
log.update({
'message': message,
'level': level.upper(),
'timestamp': self._get_timestamp()
})
return log

def bind(self, *objects):
Expand Down

0 comments on commit 1646a41

Please sign in to comment.