Skip to content

Commit e407777

Browse files
authored
Merge pull request #1471 from ceonizm/features/log_not_found
add support to access_log, error_log log_not_found per location
2 parents 8a949a0 + 666db00 commit e407777

File tree

3 files changed

+112
-4
lines changed

3 files changed

+112
-4
lines changed

manifests/resource/location.pp

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,21 @@
180180
# @param reset_timedout_connection
181181
# Enables or disables resetting timed out connections and connections closed
182182
# with the non-standard code 444.
183+
# @param format_log
184+
# Log_format to use with the defined access_log
185+
# @param access_log
186+
# Where to write access log (log format can be set with $format_log). This
187+
# can be either a string or an array; in the latter case, multiple lines will
188+
# be created. Additionally, unlike the earlier behavior, setting it to
189+
# 'absent' in the server context will remove this directive entirely from the
190+
# server stanza, rather than setting a default. Can also be disabled for this
191+
# server with the string 'off'.
192+
# @param error_log
193+
# Where to write error log. May add additional options like error level to
194+
# the end. May set to 'absent', in which case it will be omitted in this
195+
# server stanza (and default to nginx.conf setting)
196+
# @param log_not_found
197+
# Enables or disables the logging of not found errors in error_log
183198
#
184199
# @example Simple example
185200
# nginx::resource::location { 'test2.local-bob':
@@ -237,7 +252,7 @@
237252
Enum['present', 'absent'] $ensure = 'present',
238253
Boolean $internal = false,
239254
String $location = $name,
240-
Variant[String[1],Array[String[1],1]] $server = undef,
255+
Variant[String[1], Array[String[1], 1]] $server = undef,
241256
Optional[String] $www_root = undef,
242257
Optional[String] $autoindex = undef,
243258
Optional[Enum['on', 'off']] $autoindex_exact_size = undef,
@@ -305,15 +320,19 @@
305320
Optional[String] $auth_basic_user_file = undef,
306321
Optional[String] $auth_request = undef,
307322
Array $rewrite_rules = [],
308-
Integer[401,599] $priority = 500,
323+
Integer[401, 599] $priority = 500,
309324
Boolean $mp4 = false,
310325
Boolean $flv = false,
311326
Optional[String] $expires = undef,
312327
Hash $add_header = {},
313328
Optional[Enum['on', 'off', 'always']] $gzip_static = undef,
314329
Optional[Enum['on', 'off']] $reset_timedout_connection = undef,
330+
Optional[Variant[Array[String[1], 1], String[1]]] $access_log = undef,
331+
Optional[Variant[Array[String[1], 1], String[1]]] $error_log = undef,
332+
Optional[String[1]] $format_log = $nginx::http_format_log,
333+
Optional[Enum['on', 'off']] $log_not_found = undef,
315334
) {
316-
if ! defined(Class['nginx']) {
335+
if !defined(Class['nginx']) {
317336
fail('You must include the nginx base class before using any defined resources')
318337
}
319338

@@ -339,7 +358,8 @@
339358
# Use proxy, fastcgi or uwsgi template if $proxy is defined, otherwise use directory template.
340359
# fastcgi_script is deprecated
341360
if ($fastcgi_script != undef) {
342-
warning('The $fastcgi_script parameter is deprecated; please use $fastcgi_param instead to define custom fastcgi_params!')
361+
warning(
362+
'The $fastcgi_script parameter is deprecated; please use $fastcgi_param instead to define custom fastcgi_params!')
343363
}
344364

345365
# Only try to manage these files if they're the default one (as you presumably

spec/defines/resource_location_spec.rb

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,69 @@
248248
attr: 'reset_timedout_connection',
249249
value: 'on',
250250
match: %r{^\s+reset_timedout_connection\s+on;}
251+
},
252+
{
253+
title: 'access_log undef',
254+
attr: 'access_log',
255+
value: :undef,
256+
notmatch: %r{\s+access_log\s+.+;}
257+
},
258+
{
259+
title: 'disabling access_log ',
260+
attr: 'access_log',
261+
value: 'off',
262+
match: %r{\s+access_log\s+off;}
263+
},
264+
{
265+
title: 'override access_log ',
266+
attr: 'access_log',
267+
value: '/var/log/nginx/specific-location.log',
268+
match: %r{\s+access_log\s+/var/log/nginx/specific-location\.log;}
269+
},
270+
{
271+
title: 'override access_log with an array',
272+
attr: 'access_log',
273+
value: [
274+
'/var/log/nginx/specific-location.log',
275+
'syslog:server=10.0.0.1'
276+
],
277+
match: [
278+
%r{\s+access_log\s+/var/log/nginx/specific-location\.log;},
279+
%r{\s+access_log\s+syslog:server=10\.0\.0\.1\s*;}
280+
]
281+
},
282+
{
283+
title: 'enabling logging errors not found',
284+
attr: 'log_not_found',
285+
value: 'off',
286+
match: %r{\s+log_not_found\s+off;}
287+
},
288+
{
289+
title: 'enabling logging errors not found',
290+
attr: 'log_not_found',
291+
value: 'on',
292+
match: %r{\s+log_not_found\s+on;}
293+
},
294+
{
295+
title: 'should set error_log',
296+
attr: 'error_log',
297+
value: '/path/to/error.log',
298+
match: ' error_log /path/to/error.log;'
299+
},
300+
{
301+
title: 'should allow multiple error_log directives',
302+
attr: 'error_log',
303+
value: ['/path/to/error.log', 'syslog:server=localhost'],
304+
match: [
305+
' error_log /path/to/error.log;',
306+
' error_log syslog:server=localhost;'
307+
]
308+
},
309+
{
310+
title: 'should not include error_log in server when set to absent',
311+
attr: 'error_log',
312+
value: 'absent',
313+
notmatch: 'error_log'
251314
}
252315
].each do |param|
253316
context "when #{param[:attr]} is #{param[:value]}" do

templates/server/location_header.erb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,28 @@
8585
<% if @reset_timedout_connection -%>
8686
reset_timedout_connection <%= @reset_timedout_connection %>;
8787
<% end -%>
88+
<% if @log_not_found -%>
89+
log_not_found <%= @log_not_found %>;
90+
<% end -%>
91+
<% if @access_log -%>
92+
<% if @access_log.is_a?(Array) -%>
93+
<%- @access_log.each do |log_item| -%>
94+
access_log <%= log_item %><% unless @format_log.nil? -%> <%= @format_log %><% end -%>;
95+
<%- end -%>
96+
<% elsif @access_log == 'absent' -%>
97+
<% elsif @access_log == 'off' -%>
98+
access_log off;
99+
<% else -%>
100+
access_log <%= @access_log %><% unless @format_log.nil? -%> <%= @format_log %><% end -%>;
101+
<% end -%>
102+
<% end -%>
103+
<% if @error_log.is_a?(Array) -%>
104+
<%- @error_log.each do |log_item| -%>
105+
error_log <%= log_item %>;
106+
<%- end -%>
107+
<% elsif @error_log == 'absent' -%>
108+
<% elsif not @error_log -%>
109+
error_log <%= scope['::nginx::config::log_dir'] %>/<%= @name_sanitized %>.error.log;
110+
<% else -%>
111+
error_log <%= @error_log %>;
112+
<% end -%>

0 commit comments

Comments
 (0)