mirror of
https://github.com/esphome/esphome.git
synced 2025-07-28 14:16:40 +00:00
[syslog] Implement logging via syslog (#8637)
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
parent
b8d83d0765
commit
3b8a5db97c
@ -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
|
||||
|
41
esphome/components/syslog/__init__.py
Normal file
41
esphome/components/syslog/__init__.py
Normal 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]))
|
49
esphome/components/syslog/esphome_syslog.cpp
Normal file
49
esphome/components/syslog/esphome_syslog.cpp
Normal 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
|
25
esphome/components/syslog/esphome_syslog.h
Normal file
25
esphome/components/syslog/esphome_syslog.h
Normal 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
|
15
tests/components/syslog/common.yaml
Normal file
15
tests/components/syslog/common.yaml
Normal 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
|
1
tests/components/syslog/test.bk72xx-ard.yaml
Normal file
1
tests/components/syslog/test.bk72xx-ard.yaml
Normal file
@ -0,0 +1 @@
|
||||
<<: !include common.yaml
|
1
tests/components/syslog/test.esp32-ard.yaml
Normal file
1
tests/components/syslog/test.esp32-ard.yaml
Normal file
@ -0,0 +1 @@
|
||||
<<: !include common.yaml
|
1
tests/components/syslog/test.esp32-c3-ard.yaml
Normal file
1
tests/components/syslog/test.esp32-c3-ard.yaml
Normal file
@ -0,0 +1 @@
|
||||
<<: !include common.yaml
|
1
tests/components/syslog/test.esp32-c3-idf.yaml
Normal file
1
tests/components/syslog/test.esp32-c3-idf.yaml
Normal file
@ -0,0 +1 @@
|
||||
<<: !include common.yaml
|
1
tests/components/syslog/test.esp32-idf.yaml
Normal file
1
tests/components/syslog/test.esp32-idf.yaml
Normal file
@ -0,0 +1 @@
|
||||
<<: !include common.yaml
|
1
tests/components/syslog/test.esp8266-ard.yaml
Normal file
1
tests/components/syslog/test.esp8266-ard.yaml
Normal file
@ -0,0 +1 @@
|
||||
<<: !include common.yaml
|
4
tests/components/syslog/test.host.yaml
Normal file
4
tests/components/syslog/test.host.yaml
Normal file
@ -0,0 +1,4 @@
|
||||
packages:
|
||||
common: !include common.yaml
|
||||
|
||||
wifi: !remove
|
1
tests/components/syslog/test.rp2040-ard.yaml
Normal file
1
tests/components/syslog/test.rp2040-ard.yaml
Normal file
@ -0,0 +1 @@
|
||||
<<: !include common.yaml
|
Loading…
x
Reference in New Issue
Block a user