Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Issue with Python Logging in Jaclang (Version 0.7.18) #1381

Open
kashmithnisakya opened this issue Oct 15, 2024 · 3 comments
Open

[Bug] Issue with Python Logging in Jaclang (Version 0.7.18) #1381

kashmithnisakya opened this issue Oct 15, 2024 · 3 comments

Comments

@kashmithnisakya
Copy link
Contributor

Description:

I am trying to integrate Python logging in Jaclang, but I’ve encountered an issue where the log file is not created as expected.

Here’s the Python code I tested:

import logging

# Configure logging to write to a file and overwrite it each time
logging.basicConfig(
   filename='logfile.log',
   level=logging.INFO,
   format='%(asctime)s - %(levelname)s - %(message)s',
   filemode='w'  # 'w' mode ensures overwriting the file
)

# Example log entries
logging.info('This is an info message.')
logging.error('This is an error message.')

This Python code works as intended and creates a logfile.log with the following output:

2024-10-14 16:57:07,873 - INFO - This is an info message.
2024-10-14 16:57:07,873 - ERROR - This is an error message. 

However, when I try to do the same in Jac (version 0.7.18) using the following Jac code, the log file is not created:

import:py logging;

with entry:__main__{
    logging.basicConfig(
        filename='logfile.log', 
        level=logging.INFO, 
        format='%(asctime)s - %(levelname)s - %(message)s',
        filemode='w'
    );

    logging.info('This is an info message.');
    logging.error('This is an error message.');
}

Expected Behavior:

  • The logfile.log should be created, and it should contain the log entries as specified in the code.

Actual Behavior:

  • No log file is created.

Steps to Reproduce:

  1. Run the provided Jac code.
  2. Observe that no log file is generated.

Environment:

  • Jac Version: 0.7.18

Could you please look into why this issue is occurring in Jac and why it is not creating the log file as it does with the Python implementation?

Thank you!

@kugesan1105
Copy link
Collaborator

kugesan1105 commented Oct 15, 2024

@marsninja
yes, I’ve encountered the same problem where the log messages are printed to the terminal instead of being written to a file as expected
image

After looking into the jac_gen file, I suspect the issue might be with how jac_import is handling the logging module.

from __future__ import annotations
from jaclang import jac_import as __jac_import__
import typing as _jac_typ
if _jac_typ.TYPE_CHECKING:
    import logging
else:
    logging, = __jac_import__(target='logging', base_path=__file__, lng='py', absorb=False, mdl_alias=None, items={})

@ypkang
Copy link
Contributor

ypkang commented Oct 15, 2024

have you tried using the logger instance to log? something like this

import logging
logger = logging.getLogger(__name__)
logging.basicConfig(filename='example.log', encoding='utf-8', level=logging.DEBUG)
logger.debug('This message should go to the log file')
logger.info('So should this')
logger.warning('And this, too')
logger.error('And non-ASCII stuff, too, like Øresund and Malmö')

@kugesan1105
Copy link
Collaborator

Hey @ypkang,
I tried the logger instance approach as suggested, but the issue persisted—log files were not being created, and messages were still printed to the screen.

@kashmithnisakya
However, I then tried using a FileHandler directly with the logger, and that worked! But Logs are now output to both the terminal and the file:

image

import:py logging;

with entry:__main__{
    logger = logging.getLogger(__name__);
    logger.setLevel(logging.INFO);
    # Create file handler and set level
    file_handler = logging.FileHandler('logfile_jac.log', mode='w');
    file_handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'));
    # Clear any pre-existing handlers
    if logger.hasHandlers(){
        logger.handlers.clear();
    }
    # Add file handler to logger
    logger.addHandler(file_handler);
    logger.info('This is an info message.');
    logger.error('This is an error message.');
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants