There are times when you want to customize logging handlers in particular ways,
and if you use :func:`dictConfig` you may be able to do this without
subclassing. As an example, consider that you may want to set the ownership of a
-log file. On POSIX, this is easily done using :func:`shutil.chown`, but the file
+log file. On POSIX, this is easily done using :func:`os.chown`, but the file
handlers in the stdlib don't offer built-in support. You can customize handler
creation using a plain function such as::
def owned_file_handler(filename, mode='a', encoding=None, owner=None):
if owner:
+ import os, pwd, grp
+ # convert user and group names to uid and gid
+ uid = pwd.getpwnam(owner[0]).pw_uid
+ gid = grp.getgrnam(owner[1]).gr_gid
+ owner = (uid, gid)
if not os.path.exists(filename):
open(filename, 'a').close()
- shutil.chown(filename, *owner)
+ os.chown(filename, *owner)
return logging.FileHandler(filename, mode, encoding)
You can then specify, in a logging configuration passed to :func:`dictConfig`,