Last active
May 10, 2017 17:15
-
-
Save brannondorsey/99f53738f74d4ee84408b0f11ae6675f to your computer and use it in GitHub Desktop.
Simply Python Logger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Copyright (c) 2017 Brannon Dorsey | |
| # 2017 Branger Briz, Inc | |
| # | |
| # Permission is hereby granted, free of charge, to any person obtaining a copy | |
| # of this software and associated documentation files (the "Software"), to deal | |
| # in the Software without restriction, including without limitation the rights | |
| # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| # copies of the Software, and to permit persons to whom the Software is | |
| # furnished to do so, subject to the following conditions: | |
| # | |
| # The above copyright notice and this permission notice shall be included in all | |
| # copies or substantial portions of the Software. | |
| # | |
| # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
| # SOFTWARE. | |
| import os | |
| # reads value from LOG_LEVEL environment variable and conditionally | |
| # logs messages accourdingly | |
| def log(message, level, default='VERBOSE'): | |
| level = level.upper() | |
| log_level = str(os.getenv('LOG_LEVEL', default.upper())).upper() | |
| log_dict = { | |
| "VERBOSE": 1, | |
| "NOTICE": 2, | |
| "WARNING": 3, | |
| "ERROR": 4, | |
| "FATAL": 5, | |
| "SILENT": 0 | |
| } | |
| if level not in log_dict: | |
| raise Exception('{} is not a supported log level'.format(level)) | |
| if log_level not in log_dict: | |
| raise Exception('{} is not a supported log level'.format(log_level)) | |
| if log_level == 'SILENT': | |
| return | |
| elif log_dict[level] >= log_dict[log_level]: | |
| print('[{}] {}'.format(level.upper().center(7), message)) | |
| # tests the log function | |
| def test_log(): | |
| def log_test(): | |
| print('') | |
| message = 'this is a test message.' | |
| log(message, 'verbose') | |
| log(message, 'notice') | |
| log(message, 'warning') | |
| log(message, 'error') | |
| log(message, 'fatal') | |
| print('') | |
| os.environ['LOG_LEVEL'] = 'VERBOSE' | |
| print('set log level VERBOSE') | |
| log_test() | |
| os.environ['LOG_LEVEL'] = 'NOTICE' | |
| print('set log level NOTICE') | |
| log_test() | |
| os.environ['LOG_LEVEL'] = 'WARNING' | |
| print('set log level WARNING') | |
| log_test() | |
| os.environ['LOG_LEVEL'] = 'ERROR' | |
| print('set log level ERROR') | |
| log_test() | |
| os.environ['LOG_LEVEL'] = 'FATAL' | |
| print('set log level FATAL') | |
| log_test() | |
| if __name__ == '__main__': | |
| test_log() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment