Skip to content

Commit

Permalink
Update "opener" docs to use safe permissions (#953)
Browse files Browse the repository at this point in the history
Co-authored-by: Delgan <delgan.py@gmail.com>
  • Loading branch information
quantumpacket and Delgan committed Sep 3, 2023
1 parent 8c2044e commit 80bcf4e
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions docs/resources/recipes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Code snippets and recipes for ``loguru``
.. |contextlib.redirect_stdout| replace:: :func:`contextlib.redirect_stdout`
.. |copy.deepcopy| replace:: :func:`copy.deepcopy`
.. |os.fork| replace:: :func:`os.fork`
.. |os.umask| replace:: :func:`os.umask`
.. |multiprocessing| replace:: :mod:`multiprocessing`
.. |pickle| replace:: :mod:`pickle`
.. |traceback| replace:: :mod:`traceback`
Expand Down Expand Up @@ -457,15 +458,15 @@ Setting permissions on created log files

To set desired permissions on created log files, use the ``opener`` argument to pass in a custom opener with permissions octal::

os.umask(0) # Update the current umask (it's value is masked out from "os.open()" permissions)

def opener(file, flags):
return os.open(file, flags, 0o777)
return os.open(file, flags, 0o600) # read/write by owner only

logger.add("foo.log", rotation="100 kB", opener=opener)

When using an opener argument, all created log files including ones created during rotation will use the initially provided opener.

Note that the provided mode will be masked out by the OS `umask <https://en.wikipedia.org/wiki/Umask>`_ value (describing which bits are *not* to be set when creating a file or directory). This value is conventionally equals to ``0o022``, which means specifying a ``0o666`` mode will result in a ``0o666 - 0o022 = 0o644`` file permission in this case (which is actually the default). It is possible to change the umask value by first calling |os.umask|, but this needs to be done with careful consideration, as it changes the value globally and can cause security issues.


Preserving an ``opt()`` parameter for the whole module
------------------------------------------------------
Expand Down

0 comments on commit 80bcf4e

Please sign in to comment.