3

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
user-517752
  • 1,188
  • 5
  • 21
  • 54
  • Did you try making the path if it does not exist already with `if not os.path.exists(path): os.makedirs(path)`? – Caridorc Jan 12 '23 at 20:42
  • @Caridorc: Thank you much for the suggestion! The path folders `("/sim/things/t1_gateway_iotgateway_1234/greengrass/logs/")` is already exists in system. Do you still want to check it? – user-517752 Jan 12 '23 at 20:48
  • Maybe it is a problem of local vs global paths, try adding it into the code anyway and tell me it improves your results, also printing the current directory https://stackoverflow.com/questions/5137497/find-the-current-directory-and-files-directory to check if it is what you think it is – Caridorc Jan 12 '23 at 20:55
  • 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` – user-517752 Jan 12 '23 at 20:59
  • Maybe the problem is Global vs Relative paths: https://stackoverflow.com/questions/44772007/when-to-use-absolute-path-vs-relative-path-in-python – Caridorc Jan 12 '23 at 21:06
  • @Caridorc: I did `pwd` (present working directory) command and it shows `("/sim/things/t1_gateway_iotgateway_1234/greengrass/logs/")` as a full path. – user-517752 Jan 12 '23 at 21:09
  • I am not sure how to help you further... good luck with your problem anyway. – Caridorc Jan 12 '23 at 22:00
  • I'm curious what the output of the function described in [this answer](https://stackoverflow.com/a/7787832/9267296) is if you pass it your logger. You'll need to change the handler it's looking for to `RotatingFileHandler` as that's what you're using. – Edo Akse Jan 28 '23 at 07:50
  • and do it just after instantiation of the logger, and at the middle & end of your code, to see if it changes along the way... – Edo Akse Jan 28 '23 at 07:59

2 Answers2

1

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")
user-517752
  • 1,188
  • 5
  • 21
  • 54
-2

Directing the link can be a hustle, I prefer to use this simple technique of adding r before the link in quotes.

do check this:

path folders = (r"/sim/things/t1_gateway_iotgateway_1234/greengrass/logs/")

Sumedhhh
  • 1
  • 2