Skip to content

Commit 9bfb82e

Browse files
authored
Merge branch 'main' into integration-tests/clp-ffi-js
2 parents 8956e06 + 74bf5f6 commit 9bfb82e

File tree

115 files changed

+794
-1466
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

115 files changed

+794
-1466
lines changed

Cargo.lock

Lines changed: 2 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/api-server/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,5 @@ thiserror = "2.0.18"
3636
tokio = { version = "1.49.0", features = ["full"] }
3737
tower-http = { version = "0.6.8", features = ["cors"] }
3838
tracing = "0.1.44"
39-
tracing-appender = "0.2.4"
40-
tracing-subscriber = { version = "0.3.22", features = ["json", "env-filter", "fmt", "std"] }
4139
utoipa = { version = "5.4.0", features = ["axum_extras"] }
4240
utoipa-axum = "0.2.0"

components/api-server/src/bin/api_server.rs

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
use anyhow::Context;
22
use clap::Parser;
33
use clp_rust_utils::{clp_config::package, serde::yaml};
4-
use tracing_appender::{
5-
non_blocking::WorkerGuard,
6-
rolling::{RollingFileAppender, Rotation},
7-
};
8-
use tracing_subscriber::{self, fmt::writer::MakeWriterExt};
94

105
#[derive(Parser)]
116
#[command(version, about = "API Server for CLP.")]
@@ -40,29 +35,6 @@ fn read_config_and_credentials(
4035
Ok((config, credentials))
4136
}
4237

43-
fn set_up_logging() -> anyhow::Result<WorkerGuard> {
44-
let logs_directory =
45-
std::env::var("CLP_LOGS_DIR").context("Expect `CLP_LOGS_DIR` environment variable.")?;
46-
let logs_directory = std::path::Path::new(logs_directory.as_str());
47-
let file_appender =
48-
RollingFileAppender::new(Rotation::HOURLY, logs_directory, "api_server.log");
49-
let (non_blocking_writer, guard) = tracing_appender::non_blocking(file_appender);
50-
tracing_subscriber::fmt()
51-
.event_format(
52-
tracing_subscriber::fmt::format()
53-
.with_level(true)
54-
.with_target(false)
55-
.with_file(true)
56-
.with_line_number(true)
57-
.json(),
58-
)
59-
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
60-
.with_ansi(false)
61-
.with_writer(std::io::stdout.and(non_blocking_writer))
62-
.init();
63-
Ok(guard)
64-
}
65-
6638
async fn shutdown_signal() {
6739
let mut sigterm = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
6840
.expect("failed to listen for SIGTERM");
@@ -79,7 +51,7 @@ async fn main() -> anyhow::Result<()> {
7951
let args = Args::parse();
8052

8153
let (config, credentials) = read_config_and_credentials(&args)?;
82-
let _guard = set_up_logging()?;
54+
let _guard = clp_rust_utils::logging::set_up_logging("api_server.log");
8355

8456
let api_server_config = config
8557
.api_server

components/clp-mcp-server/clp_mcp_server/clp_mcp_server.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
"""CLP MCP Server entry point."""
22

33
import ipaddress
4-
import logging
5-
import os
64
import socket
75
import sys
86
from pathlib import Path
97

108
import click
119
from clp_py_utils.clp_config import ClpConfig, MCP_SERVER_COMPONENT_NAME
12-
from clp_py_utils.clp_logging import get_logger, get_logging_formatter, set_logging_level
10+
from clp_py_utils.clp_logging import configure_logging, get_logger
1311
from clp_py_utils.core import read_yaml_config_file
1412
from pydantic import ValidationError
1513

@@ -38,12 +36,8 @@ def main(host: str, port: int, config_path: Path) -> int:
3836
:param config_path: The path to server's configuration file.
3937
:return: Exit code (0 for success, non-zero for failure).
4038
"""
41-
# Setup logging to file
42-
log_file_path = Path(os.getenv("CLP_LOGS_DIR")) / "mcp_server.log"
43-
logging_file_handler = logging.FileHandler(filename=log_file_path, encoding="utf-8")
44-
logging_file_handler.setFormatter(get_logging_formatter())
45-
logger.addHandler(logging_file_handler)
46-
set_logging_level(logger, os.getenv("CLP_LOGGING_LEVEL"))
39+
# Setup optional file logging and logging level.
40+
configure_logging(logger, "mcp_server")
4741

4842
exit_code = 0
4943

components/clp-py-utils/clp_py_utils/clp_logging.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import logging
2+
import os
3+
import pathlib
24
from typing import get_args, Literal
35

46
LoggingLevel = Literal[
@@ -26,10 +28,36 @@ def get_logger(name: str):
2628
return logger
2729

2830

29-
def set_logging_level(logger: logging.Logger, level: str):
31+
def set_logging_level(logger: logging.Logger, level: str | None):
32+
if level is None:
33+
logger.setLevel(logging.INFO)
34+
return
3035
if level not in get_args(LoggingLevel):
3136
logger.warning(f"Invalid logging level: {level}, using INFO as default")
3237
logger.setLevel(logging.INFO)
3338
return
3439

3540
logger.setLevel(level)
41+
42+
43+
def configure_logging(
44+
logger: logging.Logger,
45+
component_name: str,
46+
):
47+
"""
48+
Configures file logging and the logging level for a logger using environment variables.
49+
50+
If ``CLP_LOGS_DIR`` is set, a :class:`logging.FileHandler` writing to
51+
``<CLP_LOGS_DIR>/<component_name>.log`` is added to the logger.
52+
``CLP_LOGGING_LEVEL`` is used to set the logging level (defaults to INFO if unset/invalid).
53+
54+
:param logger: The logger to configure.
55+
:param component_name: Used as the log filename stem and in log messages.
56+
"""
57+
logs_dir = os.getenv("CLP_LOGS_DIR")
58+
if logs_dir:
59+
log_file = pathlib.Path(logs_dir) / f"{component_name}.log"
60+
logging_file_handler = logging.FileHandler(filename=log_file, encoding="utf-8")
61+
logging_file_handler.setFormatter(get_logging_formatter())
62+
logger.addHandler(logging_file_handler)
63+
set_logging_level(logger, os.getenv("CLP_LOGGING_LEVEL"))

components/clp-rust-utils/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ serde_yaml = "0.9.34"
1717
sqlx = { version = "0.8.6", features = ["runtime-tokio", "mysql"] }
1818
strum = "0.28.0"
1919
thiserror = "2.0.18"
20+
tracing-appender = "0.2.4"
21+
tracing-subscriber = { version = "0.3.22", features = ["json", "env-filter", "fmt", "std"] }
2022
utoipa = { version = "5.4.0" }
2123

2224
[dev-dependencies]

components/clp-rust-utils/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ pub mod clp_config;
33
pub mod database;
44
mod error;
55
pub mod job_config;
6+
pub mod logging;
67
pub mod s3;
78
pub mod serde;
89
pub mod sqs;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use tracing_appender::{
2+
non_blocking::WorkerGuard,
3+
rolling::{RollingFileAppender, Rotation},
4+
};
5+
use tracing_subscriber::{self, fmt::writer::MakeWriterExt};
6+
7+
/// Initializes the global tracing subscriber with JSON-formatted output to stdout.
8+
///
9+
/// If the `CLP_LOGS_DIR` environment variable is set, logs are also written to a
10+
/// rolling file (`{log_filename}`) in that directory. The returned [`WorkerGuard`]
11+
/// must be held for the lifetime of the program to ensure file logs are flushed.
12+
pub fn set_up_logging(log_filename: &str) -> Option<WorkerGuard> {
13+
let subscriber = tracing_subscriber::fmt()
14+
.event_format(
15+
tracing_subscriber::fmt::format()
16+
.with_level(true)
17+
.with_target(false)
18+
.with_file(true)
19+
.with_line_number(true)
20+
.json(),
21+
)
22+
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
23+
.with_ansi(false);
24+
25+
if let Ok(logs_directory) = std::env::var("CLP_LOGS_DIR") {
26+
let logs_directory = std::path::Path::new(logs_directory.as_str());
27+
let file_appender =
28+
RollingFileAppender::new(Rotation::HOURLY, logs_directory, log_filename);
29+
let (non_blocking_writer, guard) = tracing_appender::non_blocking(file_appender);
30+
31+
subscriber
32+
.with_writer(std::io::stdout.and(non_blocking_writer))
33+
.init();
34+
Some(guard)
35+
} else {
36+
subscriber.with_writer(std::io::stdout).init();
37+
None
38+
}
39+
}

components/core/CMakeLists.txt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -308,11 +308,6 @@ if(CLP_NEED_ZSTD)
308308
# v1.4.8 is the lowest version available in the package managers of the OSes we support.
309309
find_package(zstd 1.4.8 REQUIRED)
310310
message(STATUS "Found zstd ${zstd_VERSION}")
311-
if(CLP_USE_STATIC_LIBS)
312-
set(zstd_TARGET zstd::libzstd_static)
313-
else()
314-
set(zstd_TARGET zstd::libzstd_shared)
315-
endif()
316311
endif()
317312

318313
if(CLP_NEED_LIBLZMA)
@@ -795,7 +790,7 @@ if(CLP_ENABLE_TESTS)
795790
clp::string_utils
796791
ystdlib::containers
797792
ystdlib::error_handling
798-
${zstd_TARGET}
793+
zstd::libzstd_static
799794
)
800795
target_compile_features(unitTest
801796
PRIVATE cxx_std_20

components/core/src/clp/clg/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ if(CLP_BUILD_EXECUTABLES)
166166
clp::string_utils
167167
ystdlib::containers
168168
ystdlib::error_handling
169-
${zstd_TARGET}
169+
zstd::libzstd_static
170170
)
171171
# Put the built executable at the root of the build directory
172172
set_target_properties(

0 commit comments

Comments
 (0)