48 lines
2.0 KiB
Python
Executable File
48 lines
2.0 KiB
Python
Executable File
from odoo import models, fields, api, _
|
|
import os, base64
|
|
|
|
class SyncraLogs(models.Model):
|
|
_inherit = 'syncra.welcome'
|
|
|
|
system_logs = fields.Text(string="Logs do Odoo", readonly=True)
|
|
last_log_file = fields.Binary(string="Ficheiro de Log", readonly=True)
|
|
last_log_filename = fields.Char(string="Nome do Ficheiro de Log")
|
|
|
|
def action_fetch_logs(self):
|
|
"""Lê as últimas linhas do log oficial com otimização de memória"""
|
|
log_path = '/var/log/odoo/odoo.log'
|
|
try:
|
|
if os.path.exists(log_path):
|
|
# Usamos um comando de sistema (tail) que é muito mais rápido para arquivos grandes
|
|
import subprocess
|
|
result = subprocess.run(['tail', '-n', '100', log_path], capture_output=True, text=True)
|
|
logs = result.stdout
|
|
|
|
if not logs:
|
|
self.system_logs = "O ficheiro existe mas está vazio."
|
|
else:
|
|
self.system_logs = logs
|
|
else:
|
|
self.system_logs = f"Log não encontrado em: {log_path}"
|
|
except Exception as e:
|
|
self.system_logs = f"Erro ao aceder aos logs: {str(e)}"
|
|
|
|
def action_download_last_log(self):
|
|
"""Gera o download sem comprometer a memória do servidor"""
|
|
log_path = '/var/log/odoo/odoo.log'
|
|
if os.path.exists(log_path):
|
|
with open(log_path, 'rb') as f:
|
|
# O Odoo lida bem com base64 para arquivos de texto
|
|
log_content = base64.b64encode(f.read())
|
|
|
|
self.write({
|
|
'last_log_file': log_content,
|
|
'last_log_filename': f"syncra_vps_log_{fields.Date.today()}.txt"
|
|
})
|
|
|
|
return {
|
|
'type': 'ir.actions.act_url',
|
|
'url': f'/web/content/?model={self._name}&id={self.id}&field=last_log_file&filename={self.last_log_filename}&download=true',
|
|
'target': 'self',
|
|
}
|