6

I am creating Log file for the code but I am getting the following error :

[Tue Jun 11 17:22:59 2013] [error] [client 127.0.0.1]     import mainLCF
[Tue Jun 11 17:22:59 2013] [error] [client 127.0.0.1]   File "/home/ai/Desktop/home/ubuntu/LCF/GA-LCF/mainLCF.py", line 10, in 
[Tue Jun 11 17:22:59 2013] [error] [client 127.0.0.1]     logging.basicConfig(filename='genetic.log',level=logging.DEBUG,format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
[Tue Jun 11 17:22:59 2013] [error] [client 127.0.0.1]   File "/usr/lib/python2.7/logging/__init__.py", line 1528, in basicConfig
[Tue Jun 11 17:22:59 2013] [error] [client 127.0.0.1]     hdlr = FileHandler(filename, mode)
[Tue Jun 11 17:22:59 2013] [error] [client 127.0.0.1]   File "/usr/lib/python2.7/logging/__init__.py", line 901, in __init__
[Tue Jun 11 17:22:59 2013] [error] [client 127.0.0.1]     StreamHandler.__init__(self, self._open())
[Tue Jun 11 17:22:59 2013] [error] [client 127.0.0.1]   File "/usr/lib/python2.7/logging/__init__.py", line 924, in _open
[Tue Jun 11 17:22:59 2013] [error] [client 127.0.0.1]     stream = open(self.baseFilename, self.mode)
[Tue Jun 11 17:22:59 2013] [error] [client 127.0.0.1] IOError: [Errno 13] Permission denied: '/genetic.log'

I have checked the permissions in the particular folder where I want to make the log but still getting the error . My code is : (name is mainLCF.py)

import logging
import sys


logging.basicConfig(filename='genetic.log',level=logging.DEBUG,format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
logging.debug("starting of Genetic Algorithm")

sys.path.append("/home/ai/Desktop/home/ubuntu/LCF/ws_code")

import  blackboard
from pyevolve import *
def eval_func(chromosome):
     some function here

My system's file structure is :

/ 
 home
  ai
   Desktop
     home
      ubuntu
       LCF
        ws_code                 GA-LCF
           blackboard.py             main-LCF.py

I am calling mainLCF.py from another function lcf.py which is in ws_code .

  • 2
    You're trying to write to `'/genetic.log'`, which is at the root of the filesystem, not in whatever folder you checked the permissions of. – Wooble Jun 11 '13 at 12:21
  • Why is it doing so ? Shouldn't it produce the log in whichever directory the code is present ? –  Jun 11 '13 at 12:30
  • BTW: I don't know if it is due to your code shortening, but you should reconsider the use of star imports. – b3orn Jun 11 '13 at 13:08

3 Answers3

1

You need to change the Logfile path by using logging.handlers python module . In my case I did the following stuff :

import logging
from logging.handlers import RotatingFileHandler
 import  blackboard

WEBAPP_CONSTANTS = {
'LOGFILE': '/home/ai/Desktop/home/ubuntu/LCF/GA-LCF/ga.log',
}
def getWebAppConstants(constant):
     return WEBAPP_CONSTANTS.get(constant, False)

LOGFILE = getWebAppConstants('LOGFILE')
log_handler = RotatingFileHandler(LOGFILE, maxBytes=1048576, backupCount=5)
log_handler.setFormatter(logging.Formatter( '%(asctime)s %(levelname)s: %(message)s ' '[in %(pathname)s:%(lineno)d]'))
applogger = logging.getLogger("GA")
applogger.setLevel(logging.DEBUG)
applogger.addHandler(log_handler)
applogger.debug("Starting of Genetic Algorithm")

from pyevolve import *

def eval_func(chromosome):
     some function here

and it worked. However I still don't know the reason why it was earlier trying to make genetic.log at root directory .

  • Yes .. I wanted to find the method of defining absolute path without using logging handlers but I was not able to find one. –  Jun 12 '13 at 09:49
0

Looks like logging tried to open the logfile as /genetic.log. If you pass filename as a keyword argument to logging.basicConfig it creates a FileHandler which passes it to os.path.abspath which expands the filename to an absolute path based on your current working dir. So you're either in your root dir or your code changes your current working dir.

b3orn
  • 322
  • 1
  • 5
  • However I am not in the root directory and neither my code changes the absolute path . Probably it is due to the Pyevolve library . –  Jun 11 '13 at 12:35
  • Do you have any idea as such how to direct the logfiles to a particular folder ? –  Jun 11 '13 at 12:46
  • Probably not, since logging.basicConfig is called before pyevolve is imported. How do you start mainLCF.py? – b3orn Jun 11 '13 at 12:47
  • How are you calling it form the other file? – b3orn Jun 12 '13 at 09:40
  • I am calling it as follows : import sys ; sys.append("Path name of the mainLCF.py file");import mainLCF; Now I am calling the main function of mainLCF.py from the other file. –  Jun 12 '13 at 09:47
0

Although your code seems correct, I think it's better to assign an absolute path. If you develop on your local machine and the app runs on another server, there is probably some differences, like, who calls the process.
Logs are recommended to be written to /var/log/app_name

Nyta
  • 557
  • 1
  • 7
  • 18