Skip to content

Commit 9429b3f

Browse files
authored
Merge pull request #23 from algrebe/tty-aware-logs
Tty aware logs
2 parents d3c65c7 + 5718082 commit 9429b3f

3 files changed

Lines changed: 58 additions & 14 deletions

File tree

.travis.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@ language: python
22
python:
33
- "2.7"
44
install:
5-
- "pip install ."
5+
- "pip -v install ."
66
script:
77
- echo "no tests yet"
8-
before_deploy:
8+
before_install:
99
- "sudo apt-get install pandoc"
1010
- "echo `pwd`"
11-
- "pandoc --from=markdown --to=rst --output=README.rst README.md"
11+
- "pandoc --from=markdown --to=rst --output=$TRAVIS_BUILD_DIR/README.rst README.md"
12+
- "ls -alhrt README*"
1213
deploy:
1314
# test pypi
1415
- provider: pypi
1516
distributions: "sdist bdist_wheel"
1617
server: https://testpypi.python.org/pypi
18+
skip_cleanup: true
1719
user: "deepcompute"
1820
password:
1921
secure: "fkHbFReSrTMDhMP1T26rlk0jVwmWyz8AkJqjThLWd0JBzxpAzLpeDCweCFuJk9hk1bnVnbJN/SgRBcRyYDkGCrarEtu3WkesGnIaqFKbMnu5Zwytex3ghPDK4ZK6+GVYSyljbYJUYXDZLMRoumiMOm7IHGVZ8soA02XNy7lBe36o5Dj3QfpRGUZLb2/nbMWIBvltecuhMk66yWKkSlwy0RjsOTn9XwjtTr5hGC6XD9kdC6NUeK6eSmdU/pa6DyOVSoKXEBims100g4gfOck5P8gXq8ssGVnWDkU9pvTKz085ayeepVDMygI5xug1U2zM3dTvTsRnsjr3B4FsAewfc9uh/sfFpr3v2oOIdUARf2sphJEja+0Z606s9DfxFMWHopouG9JDmzPwZBJNhJRIVW93VfODsUPejKtWJd259SfNRioj+ORP4b7wVQVfjvZM5CifDKrlMTBp1adt4bE8C/DepcezwgKLjRiGU0N0ndg7oJ9XcxUppzOwtEAPx0rLymWCz03O2gCXbr5NrtFZ/kbzPpMhDW1cnSW8zTzSQpzpXrqj5la+KIBynIp+EQZdxvM3NOyHFkpswOr1bsaFSmJ96ejrohIUq95FamQI6l8yo8iJiqROTLw6YUbcLa+F8qEP8ZdthPv98uV0ZAddduO9nysWzu2Hit3bXT4dQ3U="
@@ -23,6 +25,7 @@ deploy:
2325
# pypi
2426
- provider: pypi
2527
distributions: "sdist bdist_wheel"
28+
skip_cleanup: true
2629
user: "deepcompute"
2730
password:
2831
secure: "fkHbFReSrTMDhMP1T26rlk0jVwmWyz8AkJqjThLWd0JBzxpAzLpeDCweCFuJk9hk1bnVnbJN/SgRBcRyYDkGCrarEtu3WkesGnIaqFKbMnu5Zwytex3ghPDK4ZK6+GVYSyljbYJUYXDZLMRoumiMOm7IHGVZ8soA02XNy7lBe36o5Dj3QfpRGUZLb2/nbMWIBvltecuhMk66yWKkSlwy0RjsOTn9XwjtTr5hGC6XD9kdC6NUeK6eSmdU/pa6DyOVSoKXEBims100g4gfOck5P8gXq8ssGVnWDkU9pvTKz085ayeepVDMygI5xug1U2zM3dTvTsRnsjr3B4FsAewfc9uh/sfFpr3v2oOIdUARf2sphJEja+0Z606s9DfxFMWHopouG9JDmzPwZBJNhJRIVW93VfODsUPejKtWJd259SfNRioj+ORP4b7wVQVfjvZM5CifDKrlMTBp1adt4bE8C/DepcezwgKLjRiGU0N0ndg7oJ9XcxUppzOwtEAPx0rLymWCz03O2gCXbr5NrtFZ/kbzPpMhDW1cnSW8zTzSQpzpXrqj5la+KIBynIp+EQZdxvM3NOyHFkpswOr1bsaFSmJ96ejrohIUq95FamQI6l8yo8iJiqROTLw6YUbcLa+F8qEP8ZdthPv98uV0ZAddduO9nysWzu2Hit3bXT4dQ3U="

basescript/basescript.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,20 @@ def define_log_processors(self):
5353
"""
5454
# these processors should accept logger, method_name and event_dict
5555
# and return a new dictionary which will be passed as event_dict to the next one.
56-
return [
56+
57+
# NOTE if we are using a tty, then we must add our own timestamp.
58+
processors = []
59+
60+
if sys.stderr.isatty():
61+
processors.append(structlog.processors.TimeStamper(fmt="%Y-%m-%d %H:%M%S"))
62+
63+
processors.extend([
5764
structlog.stdlib.PositionalArgumentsFormatter(),
5865
structlog.processors.StackInfoRenderer(),
5966
structlog.processors.format_exc_info,
60-
]
67+
])
68+
69+
return processors
6170

6271
def define_log_renderer(self):
6372
"""
@@ -66,6 +75,16 @@ def define_log_renderer(self):
6675
# it must accept a logger, method_name and event_dict (just like processors)
6776
# but must return the rendered string, not a dictionary.
6877
# TODO tty logic
78+
if self.args.log_format == "json":
79+
return structlog.processors.JSONRenderer()
80+
81+
if self.args.log_format == "pretty":
82+
return structlog.dev.ConsoleRenderer()
83+
84+
# log format is None, we need to guess from the tty
85+
if sys.stderr.isatty():
86+
return structlog.dev.ConsoleRenderer()
87+
6988
return structlog.processors.JSONRenderer()
7089

7190
def define_log_pre_format_hooks(self):
@@ -117,7 +136,7 @@ def processor(logger, method_name, event_dict):
117136
structlog.configure(
118137
processors=processors,
119138
context_class=dict,
120-
logger_factory=LevelLoggerFactory(level=level),
139+
logger_factory=LevelLoggerFactory(stream=sys.stderr, level=level),
121140
wrapper_class=BoundLevelLogger,
122141
cache_logger_on_first_use=True,
123142
)
@@ -161,6 +180,13 @@ def define_baseargs(self, parser):
161180
help='Name to identify this instance')
162181
parser.add_argument('--log-level', default=self.LOG_LEVEL,
163182
help='Logging level as picked from the logging module')
183+
parser.add_argument('--log-format', default=None,
184+
# TODO add more formats
185+
choices=("json", "pretty",),
186+
help=("Force the format of the logs. By default, if the "
187+
"command is from a terminal, print colorful logs. "
188+
"Otherwise print json."),
189+
)
164190

165191
def define_args(self, parser):
166192
'''

setup.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
11
from setuptools import setup, find_packages
22
import os
33

4-
long_description = ""
5-
rst_readme = os.path.join(
6-
os.path.dirname(__file__), "README.rst"
7-
)
8-
if os.path.exists(rst_readme):
9-
with open(rst_readme) as fp:
10-
long_description = rst_readme.read()
4+
HERE = os.path.abspath(os.path.dirname(__file__))
5+
def get_long_description():
6+
dirs = [ HERE ]
7+
if os.getenv("TRAVIS"):
8+
dirs.append(os.getenv("TRAVIS_BUILD_DIR"))
9+
10+
long_description = ""
11+
12+
for d in dirs:
13+
rst_readme = os.path.join(d, "README.rst")
14+
if not os.path.exists(rst_readme):
15+
print "failed to find %s" % rst_readme
16+
continue
17+
18+
print "found rst readme %s" % rst_readme
19+
with open(rst_readme) as fp:
20+
long_description = fp.read()
21+
22+
return long_description
23+
24+
long_description = get_long_description()
1125

12-
version = '0.1.6'
26+
version = '0.1.7'
1327
setup(
1428
name="basescript",
1529
version=version,
@@ -23,6 +37,7 @@
2337
license='MIT License',
2438
install_requires=[
2539
"structlog",
40+
"colorama",
2641
],
2742
package_dir={'basescript': 'basescript'},
2843
packages=find_packages('.'),

0 commit comments

Comments
 (0)