[syslog] Implement logging via syslog (#8637)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
Clyde Stubbs 2025-05-05 14:48:13 +10:00 committed by GitHub
parent b8d83d0765
commit 3b8a5db97c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 142 additions and 0 deletions

View File

@ -429,6 +429,7 @@ esphome/components/sun/* @OttoWinter
esphome/components/sun_gtil2/* @Mat931
esphome/components/switch/* @esphome/core
esphome/components/switch/binary_sensor/* @ssieb
esphome/components/syslog/* @clydebarrow
esphome/components/t6615/* @tylermenezes
esphome/components/tc74/* @sethgirvan
esphome/components/tca9548a/* @andreashergert1984

View File

@ -0,0 +1,41 @@
import esphome.codegen as cg
from esphome.components import udp
from esphome.components.logger import LOG_LEVELS, is_log_level
from esphome.components.time import RealTimeClock
from esphome.components.udp import CONF_UDP_ID
import esphome.config_validation as cv
from esphome.const import CONF_ID, CONF_LEVEL, CONF_PORT, CONF_TIME_ID
from esphome.cpp_types import Component, Parented
CODEOWNERS = ["@clydebarrow"]
DEPENDENCIES = ["udp", "logger", "time"]
syslog_ns = cg.esphome_ns.namespace("syslog")
Syslog = syslog_ns.class_("Syslog", Component, Parented.template(udp.UDPComponent))
CONF_STRIP = "strip"
CONF_FACILITY = "facility"
CONFIG_SCHEMA = udp.UDP_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(Syslog),
cv.GenerateID(CONF_TIME_ID): cv.use_id(RealTimeClock),
cv.Optional(CONF_PORT, default=514): cv.port,
cv.Optional(CONF_LEVEL, default="DEBUG"): is_log_level,
cv.Optional(CONF_STRIP, default=True): cv.boolean,
cv.Optional(CONF_FACILITY, default=16): cv.int_range(0, 23),
}
)
async def to_code(config):
parent = await cg.get_variable(config[CONF_UDP_ID])
time = await cg.get_variable(config[CONF_TIME_ID])
cg.add(parent.set_broadcast_port(config[CONF_PORT]))
cg.add(parent.set_should_broadcast())
level = LOG_LEVELS[config[CONF_LEVEL]]
var = cg.new_Pvariable(config[CONF_ID], level, time)
await cg.register_component(var, config)
await cg.register_parented(var, parent)
cg.add(var.set_strip(config[CONF_STRIP]))
cg.add(var.set_facility(config[CONF_FACILITY]))

View File

@ -0,0 +1,49 @@
#include "esphome_syslog.h"
#include "esphome/components/logger/logger.h"
#include "esphome/core/application.h"
#include "esphome/core/time.h"
namespace esphome {
namespace syslog {
// Map log levels to syslog severity using an array, indexed by ESPHome log level (1-7)
constexpr int LOG_LEVEL_TO_SYSLOG_SEVERITY[] = {
3, // NONE
3, // ERROR
4, // WARN
5, // INFO
6, // CONFIG
7, // DEBUG
7, // VERBOSE
7 // VERY_VERBOSE
};
void Syslog::setup() {
logger::global_logger->add_on_log_callback(
[this](int level, const char *tag, const char *message) { this->log_(level, tag, message); });
}
void Syslog::log_(const int level, const char *tag, const char *message) const {
if (level > this->log_level_)
return;
// Syslog PRI calculation: facility * 8 + severity
int severity = 7;
if ((unsigned) level <= 7) {
severity = LOG_LEVEL_TO_SYSLOG_SEVERITY[level];
}
int pri = this->facility_ * 8 + severity;
auto timestamp = this->time_->now().strftime("%b %d %H:%M:%S");
unsigned len = strlen(message);
// remove color formatting
if (this->strip_ && message[0] == 0x1B && len > 11) {
message += 7;
len -= 11;
}
auto data = str_sprintf("<%d>%s %s %s: %.*s", pri, timestamp.c_str(), App.get_name().c_str(), tag, len, message);
this->parent_->send_packet((const uint8_t *) data.data(), data.size());
}
} // namespace syslog
} // namespace esphome

View File

@ -0,0 +1,25 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
#include "esphome/components/udp/udp_component.h"
#include "esphome/components/time/real_time_clock.h"
namespace esphome {
namespace syslog {
class Syslog : public Component, public Parented<udp::UDPComponent> {
public:
Syslog(int level, time::RealTimeClock *time) : log_level_(level), time_(time) {}
void setup() override;
void set_strip(bool strip) { this->strip_ = strip; }
void set_facility(int facility) { this->facility_ = facility; }
protected:
int log_level_;
void log_(int level, const char *tag, const char *message) const;
time::RealTimeClock *time_;
bool strip_{true};
int facility_{16};
};
} // namespace syslog
} // namespace esphome

View File

@ -0,0 +1,15 @@
wifi:
ssid: MySSID
password: password1
udp:
addresses: ["239.0.60.53"]
time:
platform: host
syslog:
port: 514
strip: true
level: info
facility: 16

View File

@ -0,0 +1 @@
<<: !include common.yaml

View File

@ -0,0 +1 @@
<<: !include common.yaml

View File

@ -0,0 +1 @@
<<: !include common.yaml

View File

@ -0,0 +1 @@
<<: !include common.yaml

View File

@ -0,0 +1 @@
<<: !include common.yaml

View File

@ -0,0 +1 @@
<<: !include common.yaml

View File

@ -0,0 +1,4 @@
packages:
common: !include common.yaml
wifi: !remove

View File

@ -0,0 +1 @@
<<: !include common.yaml