Skip to content

Commit c5773b6

Browse files
authored
Merge pull request #2 from jaygel179/logging-integration
Python Logging Integration
2 parents 0f35064 + e6c65e7 commit c5773b6

20 files changed

+332
-196
lines changed

.gitignore

Lines changed: 95 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,109 @@
1-
*.py[co]
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
25

3-
# Packages
4-
*.egg
5-
*.egg-info
6-
dist
7-
build
8-
eggs
9-
parts
10-
var
11-
sdist
12-
develop-eggs
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
*.egg-info/
1324
.installed.cfg
25+
*.egg
26+
MANIFEST
27+
28+
# PyInstaller
29+
# Usually these files are written by a python script from a template
30+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
31+
*.manifest
32+
*.spec
1433

1534
# Installer logs
1635
pip-log.txt
36+
pip-delete-this-directory.txt
1737

1838
# Unit test / coverage reports
39+
htmlcov/
40+
.tox/
1941
.coverage
20-
.tox
42+
.coverage.*
43+
.cache
44+
nosetests.xml
45+
coverage.xml
46+
*.cover
47+
.hypothesis/
48+
.pytest_cache/
2149

22-
#Translations
50+
# Translations
2351
*.mo
52+
*.pot
53+
54+
# Django stuff:
55+
*.log
56+
local_settings.py
57+
db.sqlite3
58+
59+
# Flask stuff:
60+
instance/
61+
.webassets-cache
62+
63+
# Scrapy stuff:
64+
.scrapy
65+
66+
# Sphinx documentation
67+
docs/_build/
2468

25-
#Mr Developer
26-
.mr.developer.cfg
69+
# PyBuilder
70+
target/
2771

28-
# virutalenvs
72+
# Jupyter Notebook
73+
.ipynb_checkpoints
74+
75+
# pyenv
76+
.python-version
77+
78+
# celery beat schedule file
79+
celerybeat-schedule
80+
81+
# SageMath parsed files
82+
*.sage.py
83+
84+
# Environments
85+
.env
2986
.venv
87+
env/
88+
venv/
89+
env*/
90+
venv*/
91+
ENV/
92+
env.bak/
93+
venv.bak/
94+
95+
# Spyder project settings
96+
.spyderproject
97+
.spyproject
98+
99+
# Rope project settings
100+
.ropeproject
101+
102+
# mkdocs documentation
103+
/site
104+
105+
# mypy
106+
.mypy_cache/
30107

31-
# debug stuff
32-
test.py
108+
# Intellij
109+
.idea

README.md

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Stackify API for Python
2-
=======
2+
=======================
33

44
[Stackify](https://stackify.com) support for Python programs.
55

@@ -35,23 +35,35 @@ export STACKIFY_API_KEY=******
3535

3636
These options can also be provided in your code:
3737
```python
38+
# Standard API
3839
import stackify
3940

4041
logger = stackify.getLogger(application="MyApp", environment="Dev", api_key=******)
4142
logger.warning('Something happened')
4243
```
4344

45+
```python
46+
# Python Logging Integration
47+
import logging
48+
import stackify
49+
50+
# your existing logging
51+
logger = logging.getLogger()
52+
...
53+
54+
stackify_handler = stackify.StackifyHandler(application="MyApp", environment="Dev", api_key=******)
55+
logger.addHandler(stackify_handler)
56+
57+
logger.warning('Something happened')
58+
```
59+
4460
## Usage
4561

4662
stackify-python handles uploads in batches of 100 messages at a time on another thread.
4763
When your program exits, it will shut the thread down and upload the remaining messages.
4864

4965
Stackify can store extra data along with your log message:
5066
```python
51-
import stackify
52-
53-
logger = stackify.getLogger()
54-
5567
try:
5668
user_string = raw_input("Enter a number: ")
5769
print("You entered", int(user_string))

requirements.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
mock==2.0.0
2+
pytest==4.3.0
3+
pytest-cov==2.6.1
4+
requests==2.21.0
5+
retrying==1.3.3

setup.cfg

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[flake8]
2+
ignore = E501, W605
3+
exclude =
4+
.git,
5+
__pycache__,
6+
build,
7+
dist,
8+
env*,
9+
venv*,
10+
setup.cfg,
11+
README.md,
12+
LICENSE.txt,
13+
requirements.txt,
14+
15+
16+
[coverage:run]
17+
include =
18+
stackify/*
19+
omit =
20+
*tests*
21+
22+
[tool:pytest]
23+
python_files=tests.py test.py test_*.py *_test.py tests_*.py *_tests.py

setup.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55

66
try:
77
from pypandoc import convert
8-
read_md = lambda f: convert(f, 'rst')
8+
read_md = lambda f: convert(f, 'rst') # noqa
99
except ImportError:
1010
print('warning: pypandoc module not found, could not convert Markdown to RST')
11-
read_md = lambda f: open(f).read()
11+
read_md = lambda f: open(f).read() # noqa
1212

1313
version_re = re.compile(r'__version__\s+=\s+(.*)')
1414

@@ -17,26 +17,25 @@
1717
version = ast.literal_eval(version_re.search(f).group(1))
1818

1919
setup(
20-
name = 'stackify',
21-
version = version,
22-
author = 'Matthew Thompson',
23-
author_email = 'chameleonator@gmail.com',
24-
packages = ['stackify'],
25-
url = 'https://github.com/stackify/stackify-api-python',
26-
license = open('LICENSE.txt').readline(),
27-
description = 'Stackify API for Python',
28-
long_description = read_md('README.md'),
29-
download_url = 'https://github.com/stackify/stackify-api-python/tarball/0.0.1',
30-
keywords = ['logging', 'stackify', 'exception'],
20+
name='stackify-api-python',
21+
version=version,
22+
author='Stackify',
23+
author_email='support@stackify.com',
24+
packages=['stackify'],
25+
url='https://github.com/stackify/stackify-api-python',
26+
license=open('LICENSE.txt').readline(),
27+
description='Stackify API for Python',
28+
long_description=read_md('README.md'),
29+
download_url='https://github.com/stackify/stackify-api-python/tarball/0.0.1',
30+
keywords=['logging', 'stackify', 'exception'],
3131
classifiers=["Programming Language :: Python"],
32-
install_requires = [
32+
install_requires=[
3333
'retrying>=1.2.3',
3434
'requests>=2.4.1'
3535
],
36-
test_suite = 'tests',
37-
tests_requires = [
36+
test_suite='tests',
37+
tests_requires=[
3838
'mock>=1.0.1',
3939
'nose==1.3.4'
4040
]
4141
)
42-

stackify/__init__.py

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,23 @@
11
"""
22
Stackify Python API
33
"""
4-
54
__version__ = '0.0.1'
65

7-
8-
API_URL = 'https://api.stackify.com'
9-
10-
READ_TIMEOUT = 5000
11-
12-
MAX_BATCH = 100
13-
14-
QUEUE_SIZE = 1000
15-
166
import logging
177
import inspect
188
import atexit
199

20-
DEFAULT_LEVEL = logging.ERROR
21-
22-
LOGGING_LEVELS = {
23-
logging.CRITICAL: 'CRITICAL',
24-
logging.ERROR: 'ERROR',
25-
logging.WARNING: 'WARNING',
26-
logging.INFO: 'INFO',
27-
logging.DEBUG: 'DEBUG',
28-
logging.NOTSET: 'NOTSET'
29-
}
10+
from stackify.application import ApiConfiguration # noqa
11+
from stackify.constants import DEFAULT_LEVEL
12+
from stackify.handler import StackifyHandler
3013

3114

3215
class NullHandler(logging.Handler):
3316
def emit(self, record):
3417
pass
3518

36-
logging.getLogger(__name__).addHandler(NullHandler())
37-
3819

39-
from stackify.application import ApiConfiguration
40-
from stackify.http import HTTPClient
41-
42-
from stackify.handler import StackifyHandler
20+
logging.getLogger(__name__).addHandler(NullHandler())
4321

4422

4523
def getLogger(name=None, auto_shutdown=True, basic_config=True, **kwargs):
@@ -77,8 +55,6 @@ def getLogger(name=None, auto_shutdown=True, basic_config=True, **kwargs):
7755
if not [isinstance(x, StackifyHandler) for x in logger.handlers]:
7856
internal_logger = logging.getLogger(__name__)
7957
internal_logger.debug('Creating handler for logger %s', name)
80-
handler = StackifyHandler(**kwargs)
81-
logger.addHandler(handler)
8258

8359
if auto_shutdown:
8460
internal_logger.debug('Registering atexit callback')
@@ -87,6 +63,9 @@ def getLogger(name=None, auto_shutdown=True, basic_config=True, **kwargs):
8763
if logger.getEffectiveLevel() == logging.NOTSET:
8864
logger.setLevel(DEFAULT_LEVEL)
8965

66+
handler = StackifyHandler(ensure_at_exit=not auto_shutdown, **kwargs)
67+
logger.addHandler(handler)
68+
9069
handler.listener.start()
9170

9271
return logger

stackify/application.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import socket
22
import os
33

4-
from stackify import API_URL
4+
from stackify.constants import API_URL
55
from stackify.formats import JSONObject
66

77

@@ -32,8 +32,7 @@ def arg_or_env(name, args, default=None):
3232
if default:
3333
return default
3434
else:
35-
raise NameError('You must specify the keyword argument {0} or '
36-
'environment variable {1}'.format(name, env_name))
35+
raise NameError('You must specify the keyword argument {0} or environment variable {1}'.format(name, env_name))
3736

3837

3938
def get_configuration(**kwargs):

stackify/constants.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import logging
2+
3+
4+
API_URL = 'https://api.stackify.com'
5+
IDENTIFY_URL = '/Metrics/IdentifyApp'
6+
LOG_SAVE_URL = '/Log/Save'
7+
API_REQUEST_INTERVAL_IN_SEC = 30
8+
9+
MAX_BATCH = 100
10+
QUEUE_SIZE = 1000
11+
READ_TIMEOUT = 5000
12+
13+
LOGGING_LEVELS = {
14+
logging.CRITICAL: 'CRITICAL',
15+
logging.ERROR: 'ERROR',
16+
logging.WARNING: 'WARNING',
17+
logging.INFO: 'INFO',
18+
logging.DEBUG: 'DEBUG',
19+
logging.NOTSET: 'NOTSET'
20+
}
21+
DEFAULT_LEVEL = logging.ERROR

0 commit comments

Comments
 (0)