1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-24 15:59:41 +02:00
portainer/app/kubernetes/views/applications/logs/logsController.js
andres-portainer 535a26412f
fix(logging): default to pretty logging [EE-4371] (#7847)
* fix(logging): default to pretty logging EE-4371

* feat(app/logs): prettify stack traces in JSON logs

* feat(nomad/logs): prettify JSON logs in log viewer

* feat(kubernetes/logs): prettigy JSON logs in log viewers

* feat(app/logs): format and color zerolog prettified logs

* fix(app/logs): pre-parse logs when they are double serialized

Co-authored-by: andres-portainer <andres-portainer@users.noreply.github.com>
Co-authored-by: LP B <xAt0mZ@users.noreply.github.com>
2022-10-20 16:33:54 +02:00

95 lines
2.8 KiB
JavaScript

import angular from 'angular';
import { concatLogsToString, formatLogs } from '@/docker/helpers/logHelper';
class KubernetesApplicationLogsController {
/* @ngInject */
constructor($async, $state, $interval, Notifications, KubernetesApplicationService, KubernetesPodService, Blob, FileSaver) {
this.$async = $async;
this.$state = $state;
this.$interval = $interval;
this.Notifications = Notifications;
this.KubernetesApplicationService = KubernetesApplicationService;
this.KubernetesPodService = KubernetesPodService;
this.Blob = Blob;
this.FileSaver = FileSaver;
this.onInit = this.onInit.bind(this);
this.stopRepeater = this.stopRepeater.bind(this);
this.getApplicationLogsAsync = this.getApplicationLogsAsync.bind(this);
}
updateAutoRefresh() {
if (this.state.autoRefresh) {
this.setUpdateRepeater();
return;
}
this.stopRepeater();
}
stopRepeater() {
if (angular.isDefined(this.repeater)) {
this.$interval.cancel(this.repeater);
this.repeater = null;
}
}
setUpdateRepeater() {
this.repeater = this.$interval(this.getApplicationLogsAsync, this.state.refreshRate);
}
downloadLogs() {
const logsAsString = concatLogsToString(this.applicationLogs);
const data = new this.Blob([logsAsString]);
this.FileSaver.saveAs(data, this.podName + '_logs.txt');
}
async getApplicationLogsAsync() {
try {
const rawLogs = await this.KubernetesPodService.logs(this.application.ResourcePool, this.podName, this.containerName);
this.applicationLogs = formatLogs(rawLogs);
} catch (err) {
this.stopRepeater();
this.Notifications.error('Failure', err, 'Unable to retrieve application logs');
}
}
async onInit() {
this.state = {
autoRefresh: false,
refreshRate: 5000, // 5 seconds
search: '',
viewReady: false,
};
const podName = this.$transition$.params().pod;
const applicationName = this.$transition$.params().name;
const namespace = this.$transition$.params().namespace;
const containerName = this.$transition$.params().container;
this.applicationLogs = [];
this.podName = podName;
this.containerName = containerName;
try {
this.application = await this.KubernetesApplicationService.get(namespace, applicationName);
await this.getApplicationLogsAsync();
} catch (err) {
this.Notifications.error('Failure', err, 'Unable to retrieve application logs');
} finally {
this.state.viewReady = true;
}
}
$onInit() {
return this.$async(this.onInit);
}
$onDestroy() {
this.stopRepeater();
}
}
export default KubernetesApplicationLogsController;
angular.module('portainer.kubernetes').controller('KubernetesApplicationLogsController', KubernetesApplicationLogsController);