Zack Williams | f1b5098 | 2018-08-26 16:35:49 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # Copyright 2018-present Open Networking Foundation |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | import multistructlog |
| 18 | import time |
| 19 | |
| 20 | # config of logging |
| 21 | logconfig = { |
| 22 | "version": 1, |
| 23 | "handlers": { |
| 24 | "console": { |
| 25 | "class": "logging.StreamHandler" |
| 26 | }, |
| 27 | "kafka": { |
| 28 | "class": "kafkaloghandler.kafkaloghandler.KafkaLogHandler", |
| 29 | "bootstrap_servers": ["test-kafka:9092"], |
| 30 | "topic": "testtopic" |
| 31 | }, |
| 32 | }, |
| 33 | |
| 34 | "loggers": { |
| 35 | "multistructlog": { |
| 36 | "handlers": ["console", "kafka"], |
| 37 | "level": "DEBUG" |
| 38 | } |
| 39 | }, |
| 40 | } |
| 41 | |
| 42 | logger = multistructlog.create_logger(logconfig) |
| 43 | |
| 44 | logger.error('Test error message') |
| 45 | |
| 46 | extra_data = { |
| 47 | "key1": "value1", |
| 48 | "key2": "value2", |
| 49 | } |
| 50 | |
| 51 | logger.info('Test info message with extra data', extra=extra_data) |
| 52 | |
| 53 | index = 0 |
| 54 | while True: |
| 55 | logger.info('Info message - loop count: %s' % index) |
| 56 | index += 1 |
| 57 | time.sleep(10) |