Skip to content

Commit

Permalink
Merge pull request #23 from algrebe/tty-aware-logs
Browse files Browse the repository at this point in the history
Tty aware logs
  • Loading branch information
algrebe authored Dec 20, 2016
2 parents d3c65c7 + 5718082 commit 9429b3f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 14 deletions.
9 changes: 6 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ language: python
python:
- "2.7"
install:
- "pip install ."
- "pip -v install ."
script:
- echo "no tests yet"
before_deploy:
before_install:
- "sudo apt-get install pandoc"
- "echo `pwd`"
- "pandoc --from=markdown --to=rst --output=README.rst README.md"
- "pandoc --from=markdown --to=rst --output=$TRAVIS_BUILD_DIR/README.rst README.md"
- "ls -alhrt README*"
deploy:
# test pypi
- provider: pypi
distributions: "sdist bdist_wheel"
server: https://testpypi.python.org/pypi
skip_cleanup: true
user: "deepcompute"
password:
secure: "fkHbFReSrTMDhMP1T26rlk0jVwmWyz8AkJqjThLWd0JBzxpAzLpeDCweCFuJk9hk1bnVnbJN/SgRBcRyYDkGCrarEtu3WkesGnIaqFKbMnu5Zwytex3ghPDK4ZK6+GVYSyljbYJUYXDZLMRoumiMOm7IHGVZ8soA02XNy7lBe36o5Dj3QfpRGUZLb2/nbMWIBvltecuhMk66yWKkSlwy0RjsOTn9XwjtTr5hGC6XD9kdC6NUeK6eSmdU/pa6DyOVSoKXEBims100g4gfOck5P8gXq8ssGVnWDkU9pvTKz085ayeepVDMygI5xug1U2zM3dTvTsRnsjr3B4FsAewfc9uh/sfFpr3v2oOIdUARf2sphJEja+0Z606s9DfxFMWHopouG9JDmzPwZBJNhJRIVW93VfODsUPejKtWJd259SfNRioj+ORP4b7wVQVfjvZM5CifDKrlMTBp1adt4bE8C/DepcezwgKLjRiGU0N0ndg7oJ9XcxUppzOwtEAPx0rLymWCz03O2gCXbr5NrtFZ/kbzPpMhDW1cnSW8zTzSQpzpXrqj5la+KIBynIp+EQZdxvM3NOyHFkpswOr1bsaFSmJ96ejrohIUq95FamQI6l8yo8iJiqROTLw6YUbcLa+F8qEP8ZdthPv98uV0ZAddduO9nysWzu2Hit3bXT4dQ3U="
Expand All @@ -23,6 +25,7 @@ deploy:
# pypi
- provider: pypi
distributions: "sdist bdist_wheel"
skip_cleanup: true
user: "deepcompute"
password:
secure: "fkHbFReSrTMDhMP1T26rlk0jVwmWyz8AkJqjThLWd0JBzxpAzLpeDCweCFuJk9hk1bnVnbJN/SgRBcRyYDkGCrarEtu3WkesGnIaqFKbMnu5Zwytex3ghPDK4ZK6+GVYSyljbYJUYXDZLMRoumiMOm7IHGVZ8soA02XNy7lBe36o5Dj3QfpRGUZLb2/nbMWIBvltecuhMk66yWKkSlwy0RjsOTn9XwjtTr5hGC6XD9kdC6NUeK6eSmdU/pa6DyOVSoKXEBims100g4gfOck5P8gXq8ssGVnWDkU9pvTKz085ayeepVDMygI5xug1U2zM3dTvTsRnsjr3B4FsAewfc9uh/sfFpr3v2oOIdUARf2sphJEja+0Z606s9DfxFMWHopouG9JDmzPwZBJNhJRIVW93VfODsUPejKtWJd259SfNRioj+ORP4b7wVQVfjvZM5CifDKrlMTBp1adt4bE8C/DepcezwgKLjRiGU0N0ndg7oJ9XcxUppzOwtEAPx0rLymWCz03O2gCXbr5NrtFZ/kbzPpMhDW1cnSW8zTzSQpzpXrqj5la+KIBynIp+EQZdxvM3NOyHFkpswOr1bsaFSmJ96ejrohIUq95FamQI6l8yo8iJiqROTLw6YUbcLa+F8qEP8ZdthPv98uV0ZAddduO9nysWzu2Hit3bXT4dQ3U="
Expand Down
32 changes: 29 additions & 3 deletions basescript/basescript.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,20 @@ def define_log_processors(self):
"""
# these processors should accept logger, method_name and event_dict
# and return a new dictionary which will be passed as event_dict to the next one.
return [

# NOTE if we are using a tty, then we must add our own timestamp.
processors = []

if sys.stderr.isatty():
processors.append(structlog.processors.TimeStamper(fmt="%Y-%m-%d %H:%M%S"))

processors.extend([
structlog.stdlib.PositionalArgumentsFormatter(),
structlog.processors.StackInfoRenderer(),
structlog.processors.format_exc_info,
]
])

return processors

def define_log_renderer(self):
"""
Expand All @@ -66,6 +75,16 @@ def define_log_renderer(self):
# it must accept a logger, method_name and event_dict (just like processors)
# but must return the rendered string, not a dictionary.
# TODO tty logic
if self.args.log_format == "json":
return structlog.processors.JSONRenderer()

if self.args.log_format == "pretty":
return structlog.dev.ConsoleRenderer()

# log format is None, we need to guess from the tty
if sys.stderr.isatty():
return structlog.dev.ConsoleRenderer()

return structlog.processors.JSONRenderer()

def define_log_pre_format_hooks(self):
Expand Down Expand Up @@ -117,7 +136,7 @@ def processor(logger, method_name, event_dict):
structlog.configure(
processors=processors,
context_class=dict,
logger_factory=LevelLoggerFactory(level=level),
logger_factory=LevelLoggerFactory(stream=sys.stderr, level=level),
wrapper_class=BoundLevelLogger,
cache_logger_on_first_use=True,
)
Expand Down Expand Up @@ -161,6 +180,13 @@ def define_baseargs(self, parser):
help='Name to identify this instance')
parser.add_argument('--log-level', default=self.LOG_LEVEL,
help='Logging level as picked from the logging module')
parser.add_argument('--log-format', default=None,
# TODO add more formats
choices=("json", "pretty",),
help=("Force the format of the logs. By default, if the "
"command is from a terminal, print colorful logs. "
"Otherwise print json."),
)

def define_args(self, parser):
'''
Expand Down
31 changes: 23 additions & 8 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
from setuptools import setup, find_packages
import os

long_description = ""
rst_readme = os.path.join(
os.path.dirname(__file__), "README.rst"
)
if os.path.exists(rst_readme):
with open(rst_readme) as fp:
long_description = rst_readme.read()
HERE = os.path.abspath(os.path.dirname(__file__))
def get_long_description():
dirs = [ HERE ]
if os.getenv("TRAVIS"):
dirs.append(os.getenv("TRAVIS_BUILD_DIR"))

long_description = ""

for d in dirs:
rst_readme = os.path.join(d, "README.rst")
if not os.path.exists(rst_readme):
print "failed to find %s" % rst_readme
continue

print "found rst readme %s" % rst_readme
with open(rst_readme) as fp:
long_description = fp.read()

return long_description

long_description = get_long_description()

version = '0.1.6'
version = '0.1.7'
setup(
name="basescript",
version=version,
Expand All @@ -23,6 +37,7 @@
license='MIT License',
install_requires=[
"structlog",
"colorama",
],
package_dir={'basescript': 'basescript'},
packages=find_packages('.'),
Expand Down

0 comments on commit 9429b3f

Please sign in to comment.