]> granicus.if.org Git - python/commitdiff
Updated cookbook entry to replace shutil.chown with os.chown.
authorVinay Sajip <vinay_sajip@yahoo.co.uk>
Wed, 22 Oct 2014 17:27:59 +0000 (18:27 +0100)
committerVinay Sajip <vinay_sajip@yahoo.co.uk>
Wed, 22 Oct 2014 17:27:59 +0000 (18:27 +0100)
Doc/howto/logging-cookbook.rst

index 07b04f9c39398063ca156680b048e181756011c7..47cf0f44a55cc0008b4e7c20067ab491a0f15b3d 100644 (file)
@@ -846,15 +846,20 @@ Customizing handlers with :func:`dictConfig`
 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`,