[Kimchi-devel] [PATCH v4][Wok] Rotate wok logs

Ramon Medeiros ramonn at linux.vnet.ibm.com
Mon Jan 25 17:10:43 UTC 2016



On 01/25/2016 03:03 PM, Aline Manera wrote:
>
>
> On 01/25/2016 11:52 AM, Ramon Medeiros wrote:
>> Use logrotate to compress and keep 10 logs of 10MB
>>
>> Signed-off-by: Ramon Medeiros <ramonn at linux.vnet.ibm.com>
>> ---
>> Changes:
>>
>> v4:
>> Add wokd.in file at logrotate, adding the logic to change the logging 
>> dir if
>> it has been changed at wok.conf
>>
>> v3:
>> Set log size to 10MB
>> Use WatchedFileHandler to avoid issues with python logging and logrotate
>>
>> v2:
>> Add logrotate as dependency
>> Fix typo in Copyright
>>
>>   Makefile.am                |  2 ++
>>   contrib/DEBIAN/control.in  |  1 +
>>   contrib/Makefile.am        |  1 +
>>   contrib/wok.spec.fedora.in |  2 ++
>>   contrib/wok.spec.suse.in   |  2 ++
>>   contrib/wokd.logrotate.in  | 28 ++++++++++++++++++++++++++++
>>   src/wok/config.py.in       |  2 ++
>>   src/wok/server.py          | 29 ++++++++++++++++++++---------
>>   8 files changed, 58 insertions(+), 9 deletions(-)
>>   create mode 100644 contrib/wokd.logrotate.in
>>
>> diff --git a/Makefile.am b/Makefile.am
>> index 278bda1..fb4db2c 100644
>> --- a/Makefile.am
>> +++ b/Makefile.am
>> @@ -140,6 +140,8 @@ install-data-local:
>>       mkdir -p $(DESTDIR)/etc/wok/
>>       $(INSTALL_DATA) src/dhparams.pem $(DESTDIR)/etc/wok/dhparams.pem
>>       touch $(DESTDIR)/etc/nginx/conf.d/wok.conf
>> +    mkdir -p $(DESTDIR)/etc/logrotate.d/
>> +    $(INSTALL_DATA) $(top_srcdir)/contrib/wokd.logrotate.in 
>> $(DESTDIR)/etc/logrotate.d/wokd.in
>>
>>   uninstall-local:
>>       @if test -f $(systemdsystemunitdir)/wokd.service; then \
>> diff --git a/contrib/DEBIAN/control.in b/contrib/DEBIAN/control.in
>> index 1f39ad8..e585080 100644
>> --- a/contrib/DEBIAN/control.in
>> +++ b/contrib/DEBIAN/control.in
>> @@ -14,6 +14,7 @@ Depends: python-cherrypy3 (>= 3.2.0),
>>            python-ldap,
>>            python-psutil (>= 0.6.0),
>>            fonts-font-awesome,
>> +         logrotate,
>
> Please, update the docs/*-deps.md files to add the logrotate dependency.
ok
>
>>            texlive-fonts-extra
>>   Build-Depends: xsltproc,
>>                  gettext,
>> diff --git a/contrib/Makefile.am b/contrib/Makefile.am
>> index ed4a006..036ec50 100644
>> --- a/contrib/Makefile.am
>> +++ b/contrib/Makefile.am
>> @@ -29,6 +29,7 @@ EXTRA_DIST = \
>>       wokd-upstart.conf.debian \
>>       wokd-upstart.conf.fedora \
>>       make-deb.sh.in \
>> +    wokd.logrotate.in \
>>       $(NULL)
>>
>>   make-deb.sh: make-deb.sh.in $(top_builddir)/config.status
>> diff --git a/contrib/wok.spec.fedora.in b/contrib/wok.spec.fedora.in
>> index 34b3ac3..612932d 100644
>> --- a/contrib/wok.spec.fedora.in
>> +++ b/contrib/wok.spec.fedora.in
>> @@ -19,6 +19,7 @@ Requires:    python-ldap
>>   Requires:    python-psutil >= 0.6.0
>>   Requires:    fontawesome-fonts
>>   Requires:    open-sans-fonts
>> +Requires:   logrotate
>>   BuildRequires:    gettext-devel
>>   BuildRequires:    libxslt
>>   BuildRequires:    openssl
>> @@ -116,6 +117,7 @@ rm -rf $RPM_BUILD_ROOT
>>   %{_sysconfdir}/nginx/conf.d/wok.conf.in
>>   %{_sysconfdir}/wok/wok.conf
>>   %{_sysconfdir}/wok/
>> +%{_sysconfdir}/logrotate.d/wokd.in
>>   %{_mandir}/man8/wokd.8.gz
>>
>>   %if 0%{?with_systemd}
>> diff --git a/contrib/wok.spec.suse.in b/contrib/wok.spec.suse.in
>> index 7fa660b..66c474b 100644
>> --- a/contrib/wok.spec.suse.in
>> +++ b/contrib/wok.spec.suse.in
>> @@ -20,6 +20,7 @@ Requires:    nginx
>>   Requires:    python-psutil >= 0.6.0
>>   Requires:    fontawesome-fonts
>>   Requires:    google-opensans-fonts
>> +Requires:   logrotate
>>   BuildRequires:    gettext-tools
>>   BuildRequires:    libxslt-tools
>>   BuildRequires:    openssl
>> @@ -94,6 +95,7 @@ rm -rf $RPM_BUILD_ROOT
>>   %{_sysconfdir}/wok/
>>   %{_sysconfdir}/nginx/conf.d/wok.conf.in
>>   %{_sysconfdir}/nginx/conf.d/wok.conf
>> +%{_sysconfdir}/logrotate.d/wokd.in
>>   %{_var}/lib/wok/
>>   %{_localstatedir}/log/wok/*
>>   %{_localstatedir}/log/wok/
>> diff --git a/contrib/wokd.logrotate.in b/contrib/wokd.logrotate.in
>> new file mode 100644
>> index 0000000..969167d
>> --- /dev/null
>> +++ b/contrib/wokd.logrotate.in
>> @@ -0,0 +1,28 @@
>> +#
>> +# Project Wok
>> +#
>> +# Copyright IBM Corp, 2016
>> +#
>> +# This library is free software; you can redistribute it and/or
>> +# modify it under the terms of the GNU Lesser General Public
>> +# License as published by the Free Software Foundation; either
>> +# version 2.1 of the License, or (at your option) any later version.
>> +#
>> +# This library is distributed in the hope that it will be useful,
>> +# but WITHOUT ANY WARRANTY; without even the implied warranty of
>> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
>> +# Lesser General Public License for more details.
>> +#
>> +# You should have received a copy of the GNU Lesser General Public
>> +# License along with this library; if not, write to the Free Software
>> +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  
>> 02110-1301  USA
>> +
>> +${log_dir}/*log {
>> +    daily
>> +    nomail
>> +    maxsize 10M
>> +    rotate 10
>> +    nomissingok
>> +    compress
>> +}
>> +
>> diff --git a/src/wok/config.py.in b/src/wok/config.py.in
>> index c6b1db2..e46602d 100644
>> --- a/src/wok/config.py.in
>> +++ b/src/wok/config.py.in
>> @@ -83,6 +83,7 @@ class Paths(object):
>>               self.src_dir = '@wokdir@'
>>               self.plugins_dir = '@wokdir@/plugins'
>>               self.mo_dir = '@prefix@/share/locale'
>
>> +            self.logrotate_dir = "@sysconfdir@/logrotate.d/"
>
> This path should be the same on those cases (installed or not), right?

not sure, How we can determine where is the log directory (when not 
installed, usually stays on <git>/log. And, if it's a devel runtime, 
developer must take care about that
>
>>           else:
>>               self.nginx_conf_dir = self.add_prefix('src/nginx')
>>               self.state_dir = self.add_prefix('data')
>> @@ -91,6 +92,7 @@ class Paths(object):
>>               self.src_dir = self.add_prefix('src/wok')
>>               self.plugins_dir = self.add_prefix('src/wok/plugins')
>>               self.mo_dir = self.add_prefix('mo')
>> +            self.logrotate_conf = ""
>>
>>           for font in FONT_FILES.keys():
>>               paths = FONTS_PATH[font]
>> diff --git a/src/wok/server.py b/src/wok/server.py
>> index c2f6c65..f69d39d 100644
>> --- a/src/wok/server.py
>> +++ b/src/wok/server.py
>> @@ -25,16 +25,17 @@ import logging
>>   import logging.handlers
>>   import os
>>
>> +from string import Template
>>   from wok import auth
>>   from wok import config
>> -from wok.config import WokConfig, PluginConfig
>> +from wok.config import config as configParser
>> +from wok.config import paths, PluginConfig, WokConfig
>>   from wok.control import sub_nodes
>>   from wok.model import model
>>   from wok.proxy import start_proxy, terminate_proxy
>>   from wok.root import WokRoot
>>   from wok.utils import get_enabled_plugins, import_class
>>
>> -
>>   LOGGING_LEVEL = {"debug": logging.DEBUG,
>>                    "info": logging.INFO,
>>                    "warning": logging.WARNING,
>> @@ -99,24 +100,34 @@ class Server(object):
>>           if dev_env:
>>               cherrypy.log.screen = True
>>
>> -        # Create handler to rotate access log file
>> -        h = logging.handlers.RotatingFileHandler(options.access_log, 
>> 'a',
>> -                                                 10000000, 1000)
>> +        # Create handler to access log file
>> +        h = logging.handlers.WatchedFileHandler(options.access_log, 
>> 'a', delay=1)
>>           h.setLevel(logLevel)
>>           h.setFormatter(cherrypy._cplogging.logfmt)
>>
>>           # Add access log file to cherrypy configuration
>>           cherrypy.log.access_log.addHandler(h)
>>
>> -        # Create handler to rotate error log file
>> -        h = logging.handlers.RotatingFileHandler(options.error_log, 
>> 'a',
>> -                                                 10000000, 1000)
>> +        # Create handler to error log file
>> +        h = logging.handlers.WatchedFileHandler(options.error_log, 
>> 'a', delay=1)
>>           h.setLevel(logLevel)
>>           h.setFormatter(cherrypy._cplogging.logfmt)
>>
>> -        # Add rotating log file to cherrypy configuration
>> +        # Add error log file to cherrypy configuration
>>           cherrypy.log.error_log.addHandler(h)
>>
>> +        # redefine logrotate configuration according to wok.conf
>> +        with open(os.path.join(paths.logrotate_dir, "wokd.in")) as 
>> template:
>> +            data = template.read()
>> +
>> +        data = Template(data)
>> +        data = 
>> data.safe_substitute(log_dir=configParser.get("logging", "log_dir"))
>> +
>> +        # Write file to be used for nginx.
>> +        config_file = open(os.path.join(paths.logrotate_dir, 
>> "wokd"), "w")
>> +        config_file.write(data)
>> +        config_file.close()
>> +
>>           # Handling running mode
>>           if not dev_env:
>>               cherrypy.config.update({'environment': 'production'})
>

-- 

Ramon Nunes Medeiros
Kimchi Developer
Linux Technology Center Brazil
IBM Systems & Technology Group
Phone : +55 19 2132 7878
ramonn at br.ibm.com




More information about the Kimchi-devel mailing list