Skip to content

Commit

Permalink
Improve app logger (#1373)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesús Ángel authored Apr 15, 2019
1 parent fd869eb commit dd6cc10
Show file tree
Hide file tree
Showing 18 changed files with 416 additions and 465 deletions.
6 changes: 6 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,9 @@
#
# ------------------------------- App privileges --------------------------------
#admin: true
#
# ------------------------------- App logging level ---------------------------------
# Set the loggin level for the Wazuh App log files.
# Default value: info
# Allowed values: info, debug
#logs.level: info
13 changes: 5 additions & 8 deletions init.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ export function initApp(server) {
const monitoringInstance = new Monitoring(server);
const indexPatternCronJobInstance = new IndexPatternCronJob(server);

log('[initApp]', `Waiting for awaitMigration()`, 'info');
log('init:initApp', `Waiting for Kibana migration jobs`, 'debug');
server.kibanaMigrator
.awaitMigration()
.then(() => {
log(
'[initApp]',
`awaitMigration() has been executed successfully`,
'info'
'init:initApp',
`Kibana migration jobs executed successfully`,
'debug'
);
Initialize(server);
WazuhElasticRouter(server);
Expand All @@ -46,9 +46,6 @@ export function initApp(server) {
WazuhUtilsRoutes(server);
})
.catch(error => {
log(
'[initApp]',
`initApp function failed due to: ${error.message || error}`
);
log('init:initApp', error.message || error);
});
}
7 changes: 7 additions & 0 deletions public/factories/wazuh-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,11 @@ export class WazuhConfig {
getConfig() {
return this.config;
}

/**
* Returns true if debug level is enabled, otherwise it returns false.
*/
isDebug() {
return ((this.config || {})['logs.level'] || false) === 'debug';
}
}
3 changes: 2 additions & 1 deletion public/services/resolves/get-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ export async function getWzConfig($q, genericReq, wazuhConfig) {
'wazuh.monitoring.replicas': 0,
'wazuh.monitoring.creation': 'd',
'wazuh.monitoring.pattern': 'wazuh-monitoring-3.x-*',
admin: true
admin: true,
'logs.level': 'info'
};

try {
Expand Down
18 changes: 6 additions & 12 deletions public/templates/settings/settings-configuration.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<td>{{key}}</td>
<td>
<input
ng-if="ctrl.editingKey === key && ctrl.configurationTypes[key] === 'string' && key !== 'wazuh.monitoring.creation'"
ng-if="ctrl.editingKey === key && ctrl.configurationTypes[key] === 'string' && key !== 'logs.level' && key !== 'wazuh.monitoring.creation'"
ng-disabled="ctrl.loadingChange" type="text" class="wz-input-text"
ng-model="ctrl.editingNewValue"></input>
<input ng-if="ctrl.editingKey === key && ctrl.configurationTypes[key] === 'number'"
Expand All @@ -52,6 +52,10 @@
ng-disabled="ctrl.loadingChange" class="wz-input-text wz-width-100"
ng-model="ctrl.editingNewValue"
ng-options="key as value for (key , value) in {'h': 'hourly', 'd': 'daily', 'w': 'weekly', 'm': 'monthly'}"></select>
<select ng-if="ctrl.editingKey === key && key === 'logs.level'"
ng-disabled="ctrl.loadingChange" class="wz-input-text wz-width-100"
ng-model="ctrl.editingNewValue"
ng-options="key as value for (key , value) in {'info': 'info', 'debug': 'debug'}"></select>
<textarea
ng-if="ctrl.editingKey === key && ctrl.configurationTypes[key] === 'object'"
ng-disabled="ctrl.loadingChange" class="wz-input-text wz-width-100"
Expand All @@ -63,17 +67,7 @@
<div ng-hide="key === 'admin' || !ctrl.configuration['admin']">
<span ng-show="!ctrl.editingKey || ctrl.editingKey !== key" tooltip="Edit"
ng-click="ctrl.setEditingKey(key, value)" class="cursor-pointer">
<svg class="euiIcon euiIcon--medium euiButtonIcon__icon" focusable="false"
aria-hidden="true" xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink" width="16" height="16"
viewBox="0 0 16 16">
<defs>
<path id="pencil-a"
d="M12.148 3.148L11 2l-9 9v3h3l9-9-1.144-1.144-8.002 7.998a.502.502 0 0 1-.708 0 .502.502 0 0 1 0-.708l8.002-7.998zM11 1c.256 0 .512.098.707.293l3 3a.999.999 0 0 1 0 1.414l-9 9A.997.997 0 0 1 5 15H2a1 1 0 0 1-1-1v-3c0-.265.105-.52.293-.707l9-9A.997.997 0 0 1 11 1zM5 14H2v-3l3 3z">
</path>
</defs>
<use xlink:href="#pencil-a"></use>
</svg>
<react-component name="EuiIcon" props="{type: 'pencil'}" />

</span>
<span ng-show="ctrl.editingKey === key && !ctrl.loadingChange" tooltip="Cancel"
Expand Down
4 changes: 3 additions & 1 deletion public/utils/config-equivalences.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,7 @@ export const configEquivalences = {
'wazuh.monitoring.pattern':
'Default index pattern to use on the app for Wazuh monitoring.',
admin:
'Enable or disable administrator requests to the Wazuh API when using the app.'
'Enable or disable administrator requests to the Wazuh API when using the app.',
'logs.level':
'Set the app logging level, allowed values are info and debug. Default is info.'
};
41 changes: 31 additions & 10 deletions server/controllers/wazuh-api-elastic.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,14 @@ export class WazuhApiElasticCtrl {
result.push(entry);
}
}

log(
'wazuh-api-elastic:getAPIEntries',
`${result.length} Wazuh API entries`,
'debug'
);
return result;
} catch (error) {
log('GET /elastic/apis', error.message || error);
log('wazuh-api-elastic:getAPIEntries', error.message || error);
return ErrorResponse(error.message || error, 2001, 500, reply);
}
}
Expand All @@ -68,10 +72,10 @@ export class WazuhApiElasticCtrl {
async deleteAPIEntries(req, reply) {
try {
const data = await this.wzWrapper.deleteWazuhAPIEntriesWithRequest(req);

log('wazuh-api-elastic:deleteAPIEntries', 'Success', 'debug');
return data;
} catch (error) {
log('DELETE /elastic/apis/{id}', error.message || error);
log('wazuh-api-elastic:deleteAPIEntries', error.message || error);
return ErrorResponse(error.message || error, 2002, 500, reply);
}
}
Expand Down Expand Up @@ -141,6 +145,7 @@ export class WazuhApiElasticCtrl {
!('url' in req.payload) ||
!('port' in req.payload)
) {
log('wazuh-api-elastic:saveAPI', 'Missing parameters');
return ErrorResponse('Missing data', 2010, 400, reply);
}

Expand All @@ -153,10 +158,17 @@ export class WazuhApiElasticCtrl {
req,
settings
);
log(
'wazuh-api-elastic:saveAPI',
`${req.payload.user}:*****@${req.payload.url}:${
req.payload.port
} entry saved successfully`,
'debug'
);

return { statusCode: 200, message: 'ok', response };
} catch (error) {
log('PUT /elastic/api', error.message || error);
log('wazuh-api-elastic:saveAPI', error.message || error);
return ErrorResponse(
`Could not save data in elasticsearch due to ${error.message || error}`,
2011,
Expand All @@ -177,10 +189,14 @@ export class WazuhApiElasticCtrl {
await this.wzWrapper.updateWazuhIndexDocument(req, req.params.id, {
doc: { cluster_info: req.payload.cluster_info }
});

log(
'wazuh-api-elastic:updateAPIHostname',
`API entry ${req.params.id} hostname updated`,
'debug'
);
return { statusCode: 200, message: 'ok' };
} catch (error) {
log('PUT /elastic/api-hostname/{id}', error.message || error);
log('wazuh-api-elastic:updateAPIHostname', error.message || error);
return ErrorResponse(
`Could not save data in elasticsearch due to ${error.message || error}`,
2012,
Expand All @@ -204,7 +220,8 @@ export class WazuhApiElasticCtrl {
!('url' in req.payload) ||
!('port' in req.payload)
) {
return ErrorResponse('Missing data', 2013, 400, reply);
log('wazuh-api-elastic:updateFullAPI', 'Missing paramaters');
return ErrorResponse('Missing parameters', 2013, 400, reply);
}

const valid = this.validateData(req.payload);
Expand All @@ -215,10 +232,14 @@ export class WazuhApiElasticCtrl {
await this.wzWrapper.updateWazuhIndexDocument(req, req.payload.id, {
doc: settings
});

log(
'wazuh-api-elastic:updateFullApi',
`API entry ${req.payload.id} updated`,
'debug'
);
return { statusCode: 200, message: 'ok' };
} catch (error) {
log('PUT /elastic/api-settings', error.message || error);
log('wazuh-api-elastic:updateFullAPI', error.message || error);
return ErrorResponse(
`Could not save data in elasticsearch due to ${error.message || error}`,
2014,
Expand Down
Loading

0 comments on commit dd6cc10

Please sign in to comment.