Add support loggin conf for panko

Add support fluentd.conf for openstack_log_appender param
with fluentd, ossyslog handlers.

Change-Id: I57b9cdacbe028ab96ee89f9a3d3fd247999a0df1
Related-Prod: PROD-19781
diff --git a/README.rst b/README.rst
index bada6d1..2a9932b 100644
--- a/README.rst
+++ b/README.rst
@@ -48,6 +48,33 @@
           - host: 10.20.0.104
             port: 11211
 
+Enhanced logging with logging.conf
+----------------------------------
+
+By default logging.conf is disabled.
+
+That is possible to enable per-binary logging.conf with new variables:
+  * openstack_log_appender - set it to true to enable log_config_append for all OpenStack services;
+  * openstack_fluentd_handler_enabled - set to true to enable FluentHandler for all Openstack services.
+
+Only WatchedFileHandler and FluentHandler are available.
+
+Also it is possible to configure this with pillar:
+
+.. note:: This works only if service doesnt run under apache-wsgi
+
+.. code-block:: yaml
+
+  panko:
+    server:
+      logging:
+        log_appender: true
+        log_handlers:
+          watchedfile:
+            enabled: true
+          fluentd:
+            enabled: true
+
 
 More information
 ================
diff --git a/metadata/service/server/cluster.yml b/metadata/service/server/cluster.yml
index d5b6fa2..2df7e3d 100644
--- a/metadata/service/server/cluster.yml
+++ b/metadata/service/server/cluster.yml
@@ -5,6 +5,9 @@
 parameters:
   _param:
     keystone_panko_endpoint_type: internalURL
+    openstack_log_appender: false
+    openstack_fluentd_handler_enabled: false
+    openstack_ossyslog_handler_enabled: false
   panko:
     server:
       enabled: true
@@ -28,3 +31,12 @@
         region: RegionOne
         tenant: service
         endpoint_type: ${_param:keystone_panko_endpoint_type}
+      logging:
+        log_appender: ${_param:openstack_log_appender}
+        log_handlers:
+          watchedfile:
+            enabled: true
+          fluentd:
+            enabled: ${_param:openstack_fluentd_handler_enabled}
+          ossyslog:
+            enabled: ${_param:openstack_ossyslog_handler_enabled}
diff --git a/metadata/service/server/single.yml b/metadata/service/server/single.yml
index 5cacf0c..950074f 100644
--- a/metadata/service/server/single.yml
+++ b/metadata/service/server/single.yml
@@ -5,6 +5,9 @@
 parameters:
   _param:
     keystone_panko_endpoint_type: internalURL
+    openstack_log_appender: false
+    openstack_fluentd_handler_enabled: false
+    openstack_ossyslog_handler_enabled: false
   panko:
     server:
       enabled: true
@@ -28,3 +31,12 @@
         region: RegionOne
         tenant: service
         endpoint_type: ${_param:keystone_panko_endpoint_type}
+      logging:
+        log_appender: ${_param:openstack_log_appender}
+        log_handlers:
+          watchedfile:
+            enabled: true
+          fluentd:
+            enabled: ${_param:openstack_fluentd_handler_enabled}
+          ossyslog:
+            enabled: ${_param:openstack_ossyslog_handler_enabled}
diff --git a/metadata/service/support.yml b/metadata/service/support.yml
index c9bb4c8..04ca353 100644
--- a/metadata/service/support.yml
+++ b/metadata/service/support.yml
@@ -9,3 +9,5 @@
         enabled: false
       sphinx:
         enabled: true
+      fluentd:
+        enabled: true
diff --git a/panko/files/logging.conf b/panko/files/logging.conf
new file mode 100644
index 0000000..aceb6f8
--- /dev/null
+++ b/panko/files/logging.conf
@@ -0,0 +1,90 @@
+{%- set log_handlers = [] -%}
+{%- for log_handler_name, log_handler_attrs in values.logging.log_handlers.items() %}
+  {%- if log_handler_attrs.get('enabled', False) %}
+    {%- do log_handlers.append(log_handler_name) -%}
+  {%- endif %}
+{%- endfor %}
+[loggers]
+keys = root, panko
+
+[handlers]
+keys = {{ log_handlers | join(", ") }}
+
+[formatters]
+keys = context, default{% if values.logging.log_handlers.get('fluentd',{}).get('enabled', False) %}, fluentd{% endif %}
+
+[logger_root]
+level = {{ values.logging.get('loggers', {}).get('root', {}).get('level', 'WARNING') }}
+handlers = {{ log_handlers | join(", ") }}
+
+[logger_amqp]
+level = {{ values.logging.get('loggers', {}).get('amqp', {}).get('level', 'WARNING') }}
+handlers = {{ log_handlers | join(", ") }}
+qualname = amqp
+
+[logger_panko]
+level = {{ values.logging.get('loggers', {}).get('panko', {}).get('level', 'INFO') }}
+handlers = {{ log_handlers | join(", ") }}
+qualname = panko
+propagate = 0
+
+[logger_amqplib]
+level = {{ values.logging.get('loggers', {}).get('amqplib', {}).get('level', 'WARNING') }}
+handlers = {{ log_handlers | join(", ") }}
+qualname = amqplib
+
+[logger_sqlalchemy]
+level = {{ values.logging.get('loggers', {}).get('sqlalchemy', {}).get('level', 'WARNING') }}
+handlers = {{ log_handlers | join(", ") }}
+qualname = sqlalchemy
+# "level = INFO" logs SQL queries.
+# "level = DEBUG" logs SQL queries and results.
+# "level = WARNING" logs neither.  (Recommended for production systems.)
+
+[logger_boto]
+level = {{ values.logging.get('loggers', {}).get('boto', {}).get('level', 'WARNING') }}
+handlers = {{ log_handlers | join(", ") }}
+qualname = boto
+
+[logger_suds]
+level = {{ values.logging.get('loggers', {}).get('suds', {}).get('level', 'INFO') }}
+handlers = {{ log_handlers | join(", ") }}
+qualname = suds
+
+[logger_eventletwsgi]
+level = {{ values.logging.get('loggers', {}).get('eventletwsgi', {}).get('level', 'WARNING') }}
+handlers = {{ log_handlers | join(", ") }}
+qualname = eventlet.wsgi.server
+
+{%- if values.logging.log_handlers.get('fluentd', {}).get('enabled', False) %}
+[handler_fluentd]
+class = fluent.handler.FluentHandler
+args = ('openstack.{{ service_name | replace("-", ".") }}', 'localhost', 24224)
+formatter = fluentd
+{%- endif %}
+
+{%- if values.logging.log_handlers.watchedfile.enabled %}
+[handler_watchedfile]
+class = handlers.WatchedFileHandler
+args = ('/var/log/panko/{{ service_name }}.log',)
+formatter = context
+{%- endif %}
+
+{% if values.logging.log_handlers.get('ossyslog', {}).get('enabled', False) -%}
+{%- set ossyslog_args = values.logging.log_handlers.ossyslog.get('args', {}) -%}
+[handler_ossyslog]
+class = oslo_log.handlers.OSSysLogHandler
+args = ( handlers.SysLogHandler.{{ ossyslog_args.get('facility', 'LOG_USER') }}, )
+formatter = context
+{%- endif %}
+
+[formatter_context]
+class = oslo_log.formatters.ContextFormatter
+
+[formatter_default]
+format = %(message)s
+
+{%- if values.logging.log_handlers.get('fluentd', {}).get('enabled', False) %}
+[formatter_fluentd]
+class = oslo_log.formatters.FluentFormatter
+{%- endif %}
\ No newline at end of file
diff --git a/panko/files/pike/panko.conf.Debian b/panko/files/pike/panko.conf.Debian
index 9b45523..0b43d1c 100644
--- a/panko/files/pike/panko.conf.Debian
+++ b/panko/files/pike/panko.conf.Debian
@@ -20,6 +20,9 @@
 # Note: This option can be changed without restarting.
 # Deprecated group/name - [DEFAULT]/log_config
 #log_config_append = <None>
+{%- if server.logging.log_appender %}
+log_config_append=/etc/panko/logging.conf
+{%- endif %}
 
 # Defines the format string for %%(asctime)s in log records. Default:
 # %(default)s . This option is ignored if log_config_append is set. (string
diff --git a/panko/map.jinja b/panko/map.jinja
index 755f31d..e24342e 100644
--- a/panko/map.jinja
+++ b/panko/map.jinja
@@ -4,6 +4,11 @@
   pkgs:
     - panko-api
   cacert_file: '/etc/ssl/certs/ca-certificates.crt'
+  logging:
+    log_appender: false
+    log_handlers:
+      watchedfile:
+        enabled: true
 
 {%- endload %}
 
diff --git a/panko/meta/fluentd.yml b/panko/meta/fluentd.yml
new file mode 100644
index 0000000..163d724
--- /dev/null
+++ b/panko/meta/fluentd.yml
@@ -0,0 +1,70 @@
+{%- from "panko/map.jinja" import server with context %}
+{%- if pillar.get('fluentd', {}).get('agent', {}).get('enabled', False) %}
+{%- set positiondb = pillar.fluentd.agent.dir.positiondb %}
+{%- set apache_wsgi = server.get('enabled', False) %}
+agent:
+  config:
+    label:
+      forward_input:
+        input:
+          generic_forward_input:
+            type: forward
+            bind: 0.0.0.0
+            port: 24224
+        match:
+          route_openstack_panko:
+            tag: openstack.panko.**
+            type: relabel
+            label: openstack_panko
+      openstack_panko_wsgi:
+        input:
+          panko_api_wsgi_in_tail:
+            type: tail
+            path:
+              - '/var/log/apache2/panko.access.log'
+              - '/var/log/apache2/panko.error.log'
+            tag: openstack.panko
+            pos_file: {{ positiondb }}/panko.wsgi.pos
+            parser:
+              type: regexp
+              time_key: Timestamp
+              time_format: '%d/%b/%Y:%H:%M:%S %z'
+              keep_time_key: false
+              # Apache format: https://regex101.com/r/WeCT7s/5
+              format: '/(?<hostname>[\w\.\-]+)\:(?<port>\d+)\s(?<http_client_ip_address>[\d\.]+)\s\-\s\-\s\[(?<Timestamp>.*)\]\s(?<Payload>\"(?<http_method>[A-Z]+)\s(?<http_url>\S+)\s(?<http_version>[.\/\dHTFSP]+)\"\s(?<http_status>\d{3})\s(?<http_response_time>\d+)\s(?<http_response_size>\d+)\s\"(?<http_referer>.*)\"\s\"(?<user_agent>.*)\")/'
+        filter:
+          add_panko_wsgi_record_fields:
+            tag: openstack.panko
+            type: record_transformer
+            enable_ruby: true
+            record:
+              - name: Severity
+                value: 6
+              - name: severity_label
+                value: INFO
+              - name: programname
+                value: panko-wsgi
+              - name: http_response_time
+                value: ${ record['http_response_time'].to_i/100000.to_f }
+        match:
+          send_to_default:
+            tag: openstack.panko
+            type: copy
+            store:
+              - type: relabel
+                label: default_output
+              - type: rewrite_tag_filter
+                rule:
+                  - name: severity_label
+                    regexp: '.'
+                    result: metric.panko_log_messages
+              - type: rewrite_tag_filter
+                rule:
+                  - name: http_status
+                    regexp: '.'
+                    result: metric.panko_openstack_http_response
+          push_to_metric:
+            tag: 'metric.**'
+            type: relabel
+            label: default_metric
+{% endif %}
diff --git a/panko/server.sls b/panko/server.sls
index 8907ebb..e5997ac 100644
--- a/panko/server.sls
+++ b/panko/server.sls
@@ -15,6 +15,38 @@
   - require:
     - pkg: panko_server_packages
 
+{% if server.logging.log_appender %}
+
+{%- if server.logging.log_handlers.get('fluentd', {}).get('enabled', False) %}
+panko_fluentd_logger_package:
+  pkg.installed:
+    - name: python-fluent-logger
+{%- endif %}
+
+/var/log/panko/panko.log:
+  file.managed:
+    - user: panko
+    - group: panko
+
+panko_general_logging_conf:
+  file.managed:
+    - name: /etc/panko/logging.conf
+    - source: salt://panko/files/logging.conf
+    - template: jinja
+    - user: panko
+    - group: panko
+    - defaults:
+        service_name: panko
+        values: {{ server }}
+    - require:
+      - pkg: panko_server_packages
+{%- if server.logging.log_handlers.get('fluentd', {}).get('enabled', False) %}
+      - pkg: panko_fluentd_logger_package
+{%- endif %}
+    - watch_in:
+      - service: panko_apache_restart
+{% endif %}
+
 panko_syncdb:
   cmd.run:
   - name: panko-dbsync
diff --git a/tests/pillar/server_cluster.sls b/tests/pillar/server_cluster.sls
index a2c1cb1..79e7732 100644
--- a/tests/pillar/server_cluster.sls
+++ b/tests/pillar/server_cluster.sls
@@ -31,6 +31,13 @@
           port: 11211
         - host: 127.0.0.1
           port: 11211
+    logging:
+      log_appender: false
+      log_handlers:
+        watchedfile:
+          enabled: true
+        fluentd:
+          enabled: false
 apache:
   server:
     enabled: true
diff --git a/tests/pillar/server_single.sls b/tests/pillar/server_single.sls
index 3ea39db..7cc93b2 100644
--- a/tests/pillar/server_single.sls
+++ b/tests/pillar/server_single.sls
@@ -27,6 +27,13 @@
       members:
         - host: 127.0.0.1
           port: 11211
+    logging:
+      log_appender: false
+      log_handlers:
+        watchedfile:
+          enabled: true
+        fluentd:
+          enabled: false
 apache:
   server:
     enabled: true