Paramerize logging.conf for all services

Paramerize logging.conf for all services which
use oslo.log

Change-Id: Ife02a56cde601a7785db7b7d19d7d3c5a34f5c4b
Related-Prod: PROD-21353
diff --git a/oslo_templates/files/logging/_logging.conf b/oslo_templates/files/logging/_logging.conf
new file mode 100644
index 0000000..6c79982
--- /dev/null
+++ b/oslo_templates/files/logging/_logging.conf
@@ -0,0 +1,100 @@
+{%- set log_handlers = [] -%}
+{%- set app_name = _data.logging.app_name -%}
+{%- for log_handler_name, log_handler_attrs in _data.logging.log_handlers.items() %}
+  {%- if log_handler_attrs.get('enabled', False) %}
+    {%- do log_handlers.append(log_handler_name) -%}
+  {%- endif %}
+{%- endfor %}
+
+[loggers]
+keys = root, {{ app_name }}
+
+[handlers]
+keys = {{ log_handlers | join(", ") }}
+
+[formatters]
+keys = context, default{% if _data.logging.log_handlers.get('fluentd', {}).get('enabled', False) %}, fluentd{% endif %}
+
+[logger_root]
+level = {{ _data.logging.get('loggers', {}).get('root', {}).get('level', 'WARNING') }}
+handlers = {{ log_handlers | join(", ") }}
+
+[logger_{{ app_name }}]
+level = {{ _data.logging.get('loggers', {}).get('{{ app_name }}', {}).get('level', 'INFO') }}
+handlers = {{ log_handlers | join(", ") }}
+qualname = {{ app_name }}
+propagate = 0
+
+[logger_amqp]
+level = {{ _data.logging.get('loggers', {}).get('amqp', {}).get('level', 'WARNING') }}
+handlers = {{ log_handlers | join(", ") }}
+qualname = amqp
+
+[logger_amqplib]
+level = {{ _data.logging.get('loggers', {}).get('amqplib', {}).get('level', 'WARNING') }}
+handlers = {{ log_handlers | join(", ") }}
+qualname = amqplib
+
+[logger_sqlalchemy]
+level = {{ _data.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 = {{ _data.logging.get('loggers', {}).get('boto', {}).get('level', 'WARNING') }}
+handlers = {{ log_handlers | join(", ") }}
+qualname = boto
+
+# NOTE(mikal): suds is used by the vmware driver, removing this will
+# cause many extraneous log lines for their tempest runs. Refer to
+# https://review.openstack.org/#/c/219225/ for details.
+[logger_suds]
+level = {{ _data.logging.get('loggers', {}).get('suds', {}).get('level', 'INFO') }}
+handlers = {{ log_handlers | join(", ") }}
+qualname = suds
+
+[logger_eventletwsgi]
+level = {{ _data.logging.get('loggers', {}).get('eventletwsgi', {}).get('level', 'WARNING') }}
+handlers = {{ log_handlers | join(", ") }}
+qualname = eventlet.wsgi.server
+
+{% if _data.logging.log_handlers.get('fluentd').get('enabled', False) -%}
+[handler_fluentd]
+class = fluent.handler.FluentHandler
+args = ('openstack.{{ service_name | replace("-", ".", 1) }}', 'localhost', 24224)
+formatter = fluentd
+{%- endif %}
+
+{% if _data.logging.log_handlers.watchedfile.enabled -%}
+[handler_watchedfile]
+class = handlers.WatchedFileHandler
+args = ('/var/log/{{ app_name }}/{{ service_name }}.log',)
+formatter = context
+{%- endif %}
+
+{% if _data.logging.log_handlers.get('ossyslog', {}).get('enabled', False) -%}
+{%- set ossyslog_args = _data.logging.log_handlers.ossyslog.get('args', {}) -%}
+[handler_ossyslog]
+class = oslo_log.handlers.OSSysLogHandler
+# the OSSysLogHandler uses 'syslog' lib, where the LOG_* facilities are already *8
+# but in the context where the args are evaluated we have access only to Python's
+# handlers.SysLogHandler.LOG_* constants that _ARE_NOT_ multiplied by 8.
+# To not have a completely magic single int in the rendered template,
+# we multiply it here.
+args = ( 8 * handlers.SysLogHandler.{{ ossyslog_args.get('facility', 'LOG_USER') }}, )
+formatter = context
+{%- endif %}
+
+[formatter_context]
+class = oslo_log.formatters.ContextFormatter
+
+[formatter_default]
+format = %(message)s
+
+{% if _data.logging.log_handlers.get('fluentd').get('enabled', False) -%}
+[formatter_fluentd]
+class = oslo_log.formatters.FluentFormatter
+{%- endif %}
diff --git a/oslo_templates/files/queens/oslo/_log.conf b/oslo_templates/files/queens/oslo/_log.conf
index 8c5fc97..d9c5b9e 100644
--- a/oslo_templates/files/queens/oslo/_log.conf
+++ b/oslo_templates/files/queens/oslo/_log.conf
@@ -20,7 +20,9 @@
 # logging_context_format_string). (string value)
 # Note: This option can be changed without restarting.
 # Deprecated group/name - [DEFAULT]/log_config
-#log_config_append = <None>
+{%- if _data.log_appender %}
+log_config_append=/etc/{{ _data.app_name }}/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.
@@ -129,4 +131,4 @@
 #rate_limit_except_level = CRITICAL
 
 # Enables or disables fatal status of deprecations. (boolean value)
-#fatal_deprecations = false
+#fatal_deprecations = false
\ No newline at end of file
diff --git a/tests/pillar/logging/_logging.sls b/tests/pillar/logging/_logging.sls
new file mode 100644
index 0000000..896022a
--- /dev/null
+++ b/tests/pillar/logging/_logging.sls
@@ -0,0 +1,12 @@
+_data:
+  logging:
+    app_name: 'nova'
+    log_appender: true
+    log_handlers:
+      watchedfile:
+        enabled: true
+      fluentd:
+        enabled: true
+      ossyslog:
+        enabled: true
+service_name: nova
\ No newline at end of file
diff --git a/tests/pillar/queens/oslo/_log.sls b/tests/pillar/queens/oslo/_log.sls
index ae30847..a7df0f9 100644
--- a/tests/pillar/queens/oslo/_log.sls
+++ b/tests/pillar/queens/oslo/_log.sls
@@ -1,5 +1,12 @@
 _data:
   debug: false
-  log_file: /path/to/file
+  log_file: 'nova.log'
+  log_dir: '/var/log/nova'
+  log_config_append: '/etc/nova/loggin.conf'
   use_syslog: true
   syslog_log_facility: INFO
+  app_name: 'nova'
+  log_appender: true
+  log_handlers:
+    watchedfile:
+      enabled: true
diff --git a/tests/run_tests.sh b/tests/run_tests.sh
index 96cb9ad..f626112 100755
--- a/tests/run_tests.sh
+++ b/tests/run_tests.sh
@@ -56,6 +56,9 @@
     - source: ${template_path}
     - context:
       _data: {{ pillar.get("_data", {}) }}
+    {%- if pillar.get('service_name') %}
+      service_name: {{ pillar.service_name }}
+    {%- endif %}
 EOF
 }