Skip to main content
Question

Python logging module not logging anything

  • 27 June 2024
  • 4 replies
  • 331 views

The logging module is not working when invoke from AA. Here is a simple code that I try to run but nothing gets log to log file. I did make sure that all privileges on the file are properly set. Any idea why this would not work? We do run a lot of python code without any problems but this small snippet is not working and not returning an error either and this is a pretty simple python script.

 

import logging

def log_test():

logging.basicConfig(filename="C:/temp/test.log", level=logging.DEBUG)

  logging.info('This message should also go to the log file')

4 replies

Badge

Hello, I’m experiencing the same issue. When I call the function within AA, it fails to execute the logs. However, if I run it outside of AA, the logging works successfully.

def test():

log_path = r'D:\RPA Bot Files\Report\LogPy.csv'
logging.basicConfig(filename=log_path,level= logging.DEBUG,format= '%(asctime)s - %(message)s',filemode = 'a')

Userlevel 1
Badge +5

Hi @rmorin, @JoseLuisCG,

 

I’m not into python but I have done some work around for the mentioned issue with logging in python.

I’m calling below script (.py file) in AA.

import logging

logging.basicConfig(
    filename='app.log',  # Log file name
    level=logging.DEBUG,  # Logging level for the file
    format='%(asctime)s %(levelname)s %(message)s'  # Log format
)
logging.info('This message should also go to the log file')

 

Code snippet is below:

Below folder is created at your .py file location. Inside the below folder compiled python file is created.

Compiled Python file:

 

If you open this compiled python file using Open Program file package in AA, the app.log file is generated with log information.

In location of the program/file: Provide only compiled python file name with extension (.pyc)

In start in path: Provide complete folder path till __pycache__

 

Hope it will help.

 

Thanks,

Hemantha Pindra

 

Badge +1

Ya this would work but not for us. From what I saw I suspect that Logging is written in C under the hood and do make use of malloc or similar memory allocation and this is probably why it is not working with AA but working when opening an app.

We make use of lots of python code within almost all our bots. This would require a lot of compiling and synchronizing on our part.  Since we had centralize the logging code in one module we implemented our own flavor of logging albeit losing access to all other Logging handlers and also having to open/write each time we log a line (not a big overhead)

Badge

When troubleshooting issues with the logging module in Python, especially when it seems like the logging is not producing any output to the log file, there are a few common areas to check:

1. Logging Configuration

Ensure that your logging configuration is correctly set up. If you're not seeing any output, it's possible that the logging level is set higher than the severity of the messages being logged, or the logging destination (file handler) might not be configured correctly.

Here's a basic example of how you might configure logging to output to a file:

```python
import logging

logging.basicConfig(filename='app.log', level=logging.DEBUG,
                    format='%(asctime)s %(levelname)s %(message)s')

Example usage
logging.debug('This is a debug message')
logging.info('This is an info message')
logging.warning('This is a warning message')
logging.error('This is an error message')
logging.critical('This is a critical message')
```

2. Logging Levels

Make sure that the logging level (`level=logging.DEBUG` in the example above) is set to capture the messages you are attempting to log. If you set the level to `logging.WARNING`, for instance, `logging.debug()` and `logging.info()` messages will not be logged.

3. Permissions

Check the permissions of the directory where your log file (`app.log` in the example) is supposed to be created. Ensure that the user running the Python script has write permissions to that directory.

4. Exception Handling

Wrap your logging calls in a try-except block to catch any potential exceptions that might be occurring silently:

```python
try:
    logging.debug('Attempting to log something')
except Exception as e:
    print(f"Logging failed with error: {e}")
```

5. Verify Logging Flow

Verify that your script is reaching the logging calls by placing print statements or using a debugger to ensure that the logging statements are being executed.

6. Absolute File Paths

Sometimes using relative paths for the log file (`filename='app.log'`) can cause issues depending on the current working directory of your script. Try using an absolute path (`filename='/path/to/your/app.log'`) to eliminate this as a potential problem.

Example Adjustment

If your initial attempt to log isn't working, you might try adjusting your approach to something like this:

```python
import logging

logging.basicConfig(filename='/absolute/path/to/your/app.log', level=logging.DEBUG,
                    format='%(asctime)s %(levelname)s %(message)s')

def main():
    logging.debug('Debug message')
    logging.info('Info message')
    logging.warning('Warning message')
    logging.error('Error message')
    logging.critical('Critical message')

if __name__ == '__main__':
    main()
```

### Conclusion

By carefully reviewing these aspects of your logging setup, you should be able to diagnose why your logging statements are not producing any output to the log file. Ensure that the script is executing the logging commands as expected and that the configuration and permissions are correctly set up.

Reply