Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem in generating logger file to a specific path in a greengrass

Tags:

python

logging

I am trying to generate a log file to a specific folder and path in greengrass v2. however the log file is created at the current directory.

The current directory at which the logger file is generated is

/sim/things/t1_gateway_iotgateway_1234/greengrass/packages/artifacts-unarchived/com.data.iot.RulesEngineCore/2.3.1-pp.38/package

Could you please help me where am I missing?

The following is my program.

import logging
from datetime import datetime
import os, sys
from logging.handlers import RotatingFileHandler

def getStandardStdOutHandler():

    formatter = logging.Formatter(
            fmt="[%(asctime)s][%(levelname)-7s][%(name)s] %(message)s (%(threadName)s[% (thread)d]:%(module)s:%(funcName)s:%(lineno)d)"
        )

     filename = datetime.now().strftime("rule_engine_%Y_%m_%d_%H_%M.log")
     path = "/sim/things/t1_gateway_iotgateway_1234/greengrass/logs/"

    _handler = RotatingFileHandler(path + filename, maxBytes=1000000, backupCount=5)
    _handler.setLevel(logging.DEBUG)
   _handler.setFormatter(formatter)
   return _handler


def getLogger(name: str):
    logger = logging.getLogger(name)

    logger.addHandler(getStandardStdOutHandler())

    return logger
like image 788
Pankesh Avatar asked Dec 10 '25 08:12

Pankesh


1 Answers

we have figured out this issue. The following is a complete program that address the path issue.

import logging
from logging.handlers import RotatingFileHandler
from datetime import datetime
import os
import logging.handlers
from sys import stdout






# rule engine log file name
filename = datetime.now().strftime("rule_engine_%Y_%m_%d_%H_%M.log")


def configure_log_file_path():

    GATEWAY_ID = 't1_gateway_iotgateway_1234'

    sim_dir = '/sim/things' + GATEWAY_ID + '/greengrass/logs'
    virtual_gateway = '/opt/greengrass/v2/logs'
    default_directory = os.path.expanduser('~/logs/')


    if os.path.exists(sim_dir):
        print(f"{sim_dir} exists")
        log_directory = '/sim/things/' +  GATEWAY_ID +  '/greengrass/logs/'

    elif os.path.exists(virtual_gateway):
        print(f"{virtual_gateway} exists")
        log_directory = '/opt/greengrass/v2/logs/'

    else: 
        if os.path.exists(default_directory):
            log_directory = default_directory
        else:
            os.makedirs(default_directory)
            print(f"{default_directory} created successfully")
            log_directory = default_directory



    file_path = os.path.join(log_directory , filename)
    return file_path


# handler to catch error and warning

def err_warning_handler():
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

    err_warning_handler = logging.StreamHandler(stream=stdout)
    err_warning_handler.setLevel(logging.WARNING)
    err_warning_handler.setFormatter(formatter)

    return err_warning_handler



def getStandardStdOutHandler():

    # create a logging format
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')


    # rotating file handler
    file_handler = RotatingFileHandler(configure_log_file_path(), maxBytes=100000, backupCount=5)
    file_handler.setLevel(logging.DEBUG)
    file_handler.setFormatter(formatter)

    return file_handler



# create a logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logger.addHandler(getStandardStdOutHandler())
logger.addHandler(err_warning_handler())

# log some messages
logger.info("Application started")
logger.debug("Debug message")
logger.warning("Warning message")
logger.error("Error message")
like image 173
Pankesh Avatar answered Dec 12 '25 21:12

Pankesh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!