Skip to content

Commit

Permalink
Create logger.py
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Jul 5, 2024
1 parent ee1ec05 commit 4a1e3dd
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions utils/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import logging
import logbook
from logbook.more import ColorizedStderrHandler
from logstash_formatter import LogstashFormatter

# Set up logging configuration
def setup_logging(config):
# Create logger
logger = logging.getLogger('my_app')

# Set logging level
logger.level = logging.DEBUG

# Create Logbook handler
handler = ColorizedStderrHandler(level=logging.DEBUG)
handler.formatter = LogstashFormatter()

# Add handler to logger
logger.handlers.append(handler)

# Set up ELK Stack logging
if config['logging']['elk_stack']['enabled']:
elk_handler = logbook.SysLogHandler(
address=(config['logging']['elk_stack']['host'], config['logging']['elk_stack']['port']),
level=logging.DEBUG
)
elk_handler.formatter = LogstashFormatter()
logger.handlers.append(elk_handler)

return logger

# Example usage
config = load_config("config.json")
logger = setup_logging(config)

logger.debug("This is a debug message")
logger.info("This is an info message")
logger.warning("This is a warning message")
logger.error("This is an error message")
logger.critical("This is a critical message")

0 comments on commit 4a1e3dd

Please sign in to comment.