Skip to content

Commit

Permalink
feat: support syslog appender (#72)
Browse files Browse the repository at this point in the history
Signed-off-by: tison <[email protected]>
  • Loading branch information
tisonkun authored Nov 12, 2024
1 parent c16d544 commit 7ab3a8c
Show file tree
Hide file tree
Showing 15 changed files with 476 additions and 148 deletions.
36 changes: 21 additions & 15 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,35 @@ on:
concurrency:
group: ${{ github.workflow }}-${{ github.event_name }}-${{ github.event.number || github.run_id }}
cancel-in-progress: true

jobs:
check:
name: Check
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
- uses: korandoru/hawkeye@v5
- name: Install toolchain
uses: dtolnay/rust-toolchain@nightly
with:
components: rustfmt,clippy
- uses: Swatinem/rust-cache@v2
- uses: taiki-e/install-action@v2
with:
tool: typos-cli,taplo-cli
- name: Check Format
run: cargo fmt --all
- name: Check Clippy
run: cargo clippy --all-targets --all-features -- -D warnings
- name: Check Toml
run: taplo format --check
- name: Check Typos
run: typos
tool: typos-cli,taplo-cli,hawkeye
- name: Check all
run: |
hawkeye check
taplo format --check
typos
cargo +nightly fmt --all
cargo +nightly clippy --all-targets --all-features -- -D warnings
test:
name: Run tests
runs-on: ubuntu-latest
strategy:
matrix:
os: [ ubuntu-22.04, macos-14, windows-2022 ]
rust-version: [ "1.75.0", "stable" ]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- uses: Swatinem/rust-cache@v2
Expand All @@ -67,20 +69,24 @@ jobs:
uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.rust-version }}
- name: Build
run: cargo build --workspace --all-features --bins --tests --examples --benches --lib
- name: Run unit tests
shell: bash
run: cargo test --all-features -- --nocapture
- name: Run examples
shell: bash
run: |
cargo run --example simple_stdout
cargo run --example multiple_dispatches
cargo run --example custom_layout_filter
cargo run --features="no-color" --example simple_stdout
cargo run --features="json" --example json_stdout
cargo run --features="json,rolling_file" --example rolling_file
cargo run --features="json,rolling-file" --example rolling_file
required:
name: Required
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
if: ${{ always() }}
needs:
- check
Expand Down
78 changes: 35 additions & 43 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,83 +35,75 @@ rustdoc-args = ["--cfg", "docsrs"]
fastrace = ["dep:fastrace"]
json = ["dep:serde_json", "dep:serde", "jiff/serde"]
no-color = ["colored/no-color"]
non-blocking = ["dep:crossbeam-channel"]
opentelemetry = [
"dep:opentelemetry",
"dep:opentelemetry-otlp",
"dep:opentelemetry_sdk",
]
rolling_file = ["dep:crossbeam-channel", "dep:parking_lot"]
rolling-file = ["non-blocking"]
syslog = ["non-blocking", "dep:fasyslog"]

[dependencies]
anyhow = { version = "1.0" }
colored = { version = "2.1" }
env_filter = { version = "0.1" }
jiff = { version = "0.1.13" }
jiff = { version = "0.1.14" }
log = { version = "0.4", features = ["std", "kv_unstable"] }

# Optional dependencies
crossbeam-channel = { version = "0.5", optional = true }
fastrace = { version = "0.7", optional = true }
fasyslog = { version = "0.1.1", optional = true }
opentelemetry = { version = "0.27", features = ["logs"], optional = true }
opentelemetry-otlp = { version = "0.27", features = [
"logs",
"grpc-tonic",
"http-json",
"http-proto",
], optional = true }
opentelemetry_sdk = { version = "0.27", features = [
"logs",
"rt-tokio",
], optional = true }
serde = { version = "1.0", features = ["derive"], optional = true }
serde_json = { version = "1.0", optional = true }

[dev-dependencies]
rand = "0.8"
tempfile = "3.13"
tokio = { version = "1", features = ["rt-multi-thread"] }

## Serde dependencies
[dependencies.serde]
features = ["derive"]
optional = true
version = "1.0"

[dependencies.serde_json]
optional = true
version = "1.0"

## Rolling file dependencies
[dependencies.crossbeam-channel]
optional = true
version = "0.5"

[dependencies.parking_lot]
optional = true
version = "0.12"

## Fastrace dependencies
[dependencies.fastrace]
optional = true
version = "0.7"

## Opentelemetry dependencies
[dependencies.opentelemetry]
features = ["logs"]
optional = true
version = "0.26"

[dependencies.opentelemetry-otlp]
features = ["logs", "grpc-tonic", "http-json", "http-proto"]
optional = true
version = "0.26"

[dependencies.opentelemetry_sdk]
features = ["logs", "rt-tokio"]
optional = true
version = "0.26"

## Examples
[[example]]
doc-scrape-examples = true
name = "simple_stdout"
path = "examples/simple_stdout.rs"

[[example]]
doc-scrape-examples = true
name = "json_stdout"
path = "examples/json_stdout.rs"
required-features = ["json"]

[[example]]
doc-scrape-examples = true
name = "multiple_dispatches"
path = "examples/multiple_dispatches.rs"

[[example]]
doc-scrape-examples = true
name = "rolling_file"
path = "examples/rolling_file.rs"
required-features = ["rolling_file", "json"]
required-features = ["rolling-file", "json"]

[[example]]
doc-scrape-examples = true
name = "custom_layout_filter"
path = "examples/custom_layout_filter.rs"

[[example]]
doc-scrape-examples = true
name = "syslog"
path = "examples/syslog.rs"
required-features = ["syslog"]
4 changes: 2 additions & 2 deletions examples/rolling_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use logforth::append::rolling_file::NonBlockingBuilder;
use logforth::append::rolling_file;
use logforth::append::rolling_file::RollingFile;
use logforth::append::rolling_file::RollingFileWriter;
use logforth::append::rolling_file::Rotation;
Expand All @@ -25,7 +25,7 @@ fn main() {
.build("logs")
.unwrap();

let (non_blocking, _guard) = NonBlockingBuilder::default().finish(rolling_writer);
let (non_blocking, _guard) = rolling_file::non_blocking_builder().finish(rolling_writer);

logforth::builder()
.dispatch(|d| {
Expand Down
43 changes: 43 additions & 0 deletions examples/syslog.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2024 FastLabs Developers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use logforth::append::syslog;
use logforth::append::syslog::Syslog;
use logforth::append::syslog::SyslogWriter;

fn main() {
let syslog_writer = SyslogWriter::tcp_well_known().unwrap();
let (non_blocking, _guard) = syslog::non_blocking_builder().finish(syslog_writer);

logforth::builder()
.dispatch(|d| {
d.filter(log::LevelFilter::Trace)
.append(Syslog::new(non_blocking))
})
.apply();

let repeat = 1;

for i in 0..repeat {
log::error!("Hello syslog error!");
log::warn!("Hello syslog warn!");
log::info!("Hello syslog info!");
log::debug!("Hello syslog debug!");
log::trace!("Hello syslog trace!");

if i + 1 < repeat {
std::thread::sleep(std::time::Duration::from_secs(10));
}
}
}
6 changes: 4 additions & 2 deletions src/append/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ use std::fmt;
mod fastrace;
#[cfg(feature = "opentelemetry")]
pub mod opentelemetry;
#[cfg(feature = "rolling_file")]
#[cfg(feature = "rolling-file")]
pub mod rolling_file;
mod stdio;
#[cfg(feature = "syslog")]
pub mod syslog;

#[cfg(feature = "fastrace")]
pub use self::fastrace::FastraceEvent;
#[cfg(feature = "opentelemetry")]
pub use self::opentelemetry::OpentelemetryLog;
#[cfg(feature = "rolling_file")]
#[cfg(feature = "rolling-file")]
pub use self::rolling_file::RollingFile;
pub use self::stdio::Stderr;
pub use self::stdio::Stdout;
Expand Down
Loading

0 comments on commit 7ab3a8c

Please sign in to comment.