Move logging configuration to be per-domain (#399)

* chore: per-domain logging

* fix: lint

* fix: revert cdn configuration disabling the access log

* feat: more granular controls for logging

* chore(cr): bump copyright year to 2022

* fix(cr): missing error_log level in the global config

* fix(cr): `is-changed` indicators

* chore(cr): newline at end of file + eslint enforcement

* fix(cr): rows alignment when checkbox applies

* fix(cr): don't use default computed values

* fix: lint

* chore: use new flag names to allow backward compatability

* chore: global `access_log` should always be `off`

* feat: migrate old logging to new

* feat: option to turn on access_log and error_log on redirects

* fix: update copyright year

* fix: missing translation

* fix(cr): migration from global `error_log` being empty

* fix(cr): missing `return`

* fix(cr): account for a `server` dictionary without `domain`

* fix(cr): migrate previous `access_log` and `error_log` paths using the previous behavior

* chore(cr): additional logging comment

* feat(cr): disable error_log per domain

* fix(logging): use default paths

* fix(logging): retain the user values for error_log when toggling the log on/off

* fix(bc): new params shouldn't be overridden
This commit is contained in:
Kobi Meirson
2022-11-14 17:37:44 +02:00
committed by GitHub
parent f44832ed7a
commit e2a95a5ed4
31 changed files with 406 additions and 77 deletions

View File

@@ -1,5 +1,5 @@
/*
Copyright 2020 DigitalOcean
Copyright 2022 DigitalOcean
This code is licensed under the MIT License.
You may obtain a copy of the License at
@@ -24,10 +24,4 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
export const getAccessLogDomainPath = (domain, global) => {
return global.logging.accessLog.computed.replace(/([^/]+)\.log$/, `${domain.server.domain.computed}.$1.log`);
};
export const getErrorLogDomainPath = (domain, global) => {
return global.logging.errorLog.computed.replace(/([^/]+)\.log (.+)$/, `${domain.server.domain.computed}.$1.log $2`);
};
export const serverDomainDefault = 'example.com';

View File

@@ -0,0 +1,55 @@
/*
Copyright 2022 DigitalOcean
This code is licensed under the MIT License.
You may obtain a copy of the License at
https://github.com/digitalocean/nginxconfig.io/blob/master/LICENSE or https://mit-license.org/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
export const accessLogPathDefault = '/var/log/nginx/access.log';
export const accessLogParamsDefault = 'buffer=512k flush=1m';
export const errorLogPathDefault = '/var/log/nginx/error.log';
export const errorLogPathDisabled = '/dev/null';
export const errorLogLevelDefault = 'warn';
export const errorLogLevelOptions = Object.freeze(['debug', 'info', 'notice', 'warn', 'error', 'crit', 'alert', 'emerg']);
export const errorLogLevelDisabled = 'none';
export const getDomainAccessLog = (domain, global) => {
let path = domain.logging.accessLogPath.computed.trim();
if (!path) {
path = accessLogPathDefault;
}
return path +
(global.logging.cloudflare.computed ? ' cloudflare' : '') +
(domain.logging.accessLogParameters.computed.trim() ? ` ${domain.logging.accessLogParameters.computed.trim()}`: '');
};
export const getDomainErrorLog = (domain) => {
let path = domain.logging.errorLogPath.computed.trim();
if (!path) {
path = errorLogPathDefault;
}
const errorLogLevel = errorLogLevelOptions.includes(domain.logging.errorLogLevel.computed) ? ` ${domain.logging.errorLogLevel.computed}` : '';
return `${path}${errorLogLevel}`;
};

View File

@@ -1,5 +1,5 @@
/*
Copyright 2021 DigitalOcean
Copyright 2022 DigitalOcean
This code is licensed under the MIT License.
You may obtain a copy of the License at
@@ -26,6 +26,71 @@ THE SOFTWARE.
import isObject from './is_object';
import deepMerge from './deep_merge';
import { accessLogPathDefault, accessLogParamsDefault, errorLogPathDefault, errorLogPathDisabled, errorLogLevelDefault } from './logging';
import { serverDomainDefault } from './defaults';
// Migrate old logging settings to new ones
const migrateLogging = data => {
if (Object.keys(data).length === 0) return;
const globalLogging = 'logging' in data.global && isObject(data.global.logging) ? data.global.logging : {};
// global access_log
const [globalAccessLogPath, ...globalAccessLogParameters] = (globalLogging.accessLog || accessLogPathDefault).split(' ');
const globalAccessLogEnabled =
!('accessLog' in globalLogging) || // accessLog was enabled by default and might not appear at all
(globalAccessLogPath !== '' && globalAccessLogPath !== 'off'); // *or* someone turned it off explicitly
// global error_log
const [globalErrorLogPath, ...globalErrorLogLevel] = (globalLogging.errorLog || `${errorLogPathDefault} ${errorLogLevelDefault}`).split(' ');
const globalErrorLogEnabled =
!('errorLog' in globalLogging) || // errorLog was enabled by default and might not appear at all
(globalErrorLogPath !== '' && globalErrorLogPath !== errorLogPathDisabled); // *or* someone turned it off explicitly
// set global access_log / error_log files for every domain UNLESS it was explicitly
// enabled for the domain
for (const key in data.domains) {
if (!Object.prototype.hasOwnProperty.call(data.domains, key)) continue;
const perDomainServer = {
domain: serverDomainDefault,
...('server' in data.domains[key] && isObject(data.domains[key].server) ? data.domains[key].server : {}),
};
const perDomainLogging = 'logging' in data.domains[key] && isObject(data.domains[key].logging) ? data.domains[key].logging : {};
// access_log
let accessLogEnabled = globalAccessLogEnabled,
accessLogPath = globalAccessLogPath;
const accessLogParameters = globalAccessLogParameters.join(' ') || accessLogParamsDefault;
const perDomainAccessLogEnabled = !!perDomainLogging.accessLog;
if (perDomainAccessLogEnabled) {
accessLogEnabled = true;
accessLogPath = accessLogPath.replace(/([^/]+)\.log$/, `${perDomainServer.domain}.$1.log`);
}
// error_log
let errorLogEnabled = globalErrorLogEnabled,
errorLogPath = globalErrorLogPath;
const errorLogLevel = globalErrorLogLevel.join(' ') || errorLogLevelDefault;
const perDomainErrorLogEnabled = !!perDomainLogging.errorLog;
if (perDomainErrorLogEnabled) {
errorLogEnabled = true;
errorLogPath = errorLogPath.replace(/([^/]+)\.log$/, `${perDomainServer.domain}.$1.log`);
}
data.domains[key].logging = {
accessLogEnabled,
accessLogPath,
accessLogParameters,
errorLogEnabled,
errorLogPath,
errorLogLevel,
...perDomainLogging,
};
}
};
// Handle converting the old query format to our new query params
export default data => {
@@ -68,4 +133,6 @@ export default data => {
deepMerge(data.domains[key], mappedData);
}
}
migrateLogging(data);
};