Skip to content

Commit 9f47c17

Browse files
authored
Feature/new logger (#14)
* feat: add new logger package using tokio tracing * feat: adjustments to use new logger module * feat: export serde_json declaration * chore: update examples * chore: update README file * docs: update definitions documentation
1 parent ecf0aba commit 9f47c17

File tree

32 files changed

+413
-108
lines changed

32 files changed

+413
-108
lines changed

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ edition = "2021"
66
[dependencies]
77
async-trait = "0.1.83"
88
axum = "0.7.7"
9+
chrono = "0.4.38"
910
env-settings = "0.1.11"
1011
env-settings-derive = "0.1.11"
1112
env-settings-utils = "0.1.11"
1213
futures = "0.3.31"
1314
http = "1.1.0"
14-
logger = { git = "https://github.com/rsfreitas/logger" }
15+
indexmap = { version = "2.7.0", features = ["serde"]}
1516
prost = "0.13.3"
1617
serde = "1.0.214"
1718
serde_derive = "1.0.214"
@@ -21,4 +22,6 @@ toml = "0.8.19"
2122
tonic = { version = "0.12.3", features = ["transport"]}
2223
tower = "0.5.1"
2324
tower-service = "0.3.3"
25+
tracing = "0.1.41"
26+
tracing-subscriber = { version = "0.3", features = ["env-filter", "json"] }
2427
validator = { version = "0.19.0", features = ["derive"] }

README.md

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -109,28 +109,20 @@ product = "Matrix"
109109
When executed, it outputs the following (with a different time according the execution):
110110

111111
```bash
112-
2024-11-21 22:08:04.415725 -03:00 INFO service starting {"local.ts":1732237684,"svc.version":"v0.1.0","svc.language":"rust","local.ts_ms":1732237684415,"svc.name":"script-example","svc.product":"Matrix"}
113-
2024-11-21 22:08:04.416153 -03:00 INFO starting features {"local.ts":1732237684,"svc.version":"v0.1.0","svc.language":"rust","local.ts_ms":1732237684416,"svc.name":"script-example","svc.product":"Matrix"}
114-
lifecycle on_start
115-
2024-11-21 22:08:04.416174 -03:00 INFO service resources {"local.ts":1732237684,"svc.version":"v0.1.0","svc.language":"rust","local.ts_ms":1732237684416,"svc.name":"script-example","svc.product":"Matrix"}
116-
2024-11-21 22:08:04.416194 -03:00 INFO service is running {"local.ts":1732237684,"svc.version":"v0.1.0","svc.language":"rust","local.ts_ms":1732237684416,"kind":"script","svc.name":"script-example","svc.product":"Matrix"}
117-
2024-11-21 22:08:04.416218 -03:00 INFO Stop script service {"local.ts":1732237684,"svc.version":"v0.1.0","svc.language":"rust","local.ts_ms":1732237684416,"svc.name":"script-example","svc.product":"Matrix"}
118-
2024-11-21 22:08:04.416234 -03:00 DEBUG sending shutdown signal for service tasks {"local.ts":1732237684,"svc.version":"v0.1.0","svc.language":"rust","local.ts_ms":1732237684416,"svc.name":"script-example","svc.product":"Matrix"}
119-
2024-11-21 22:08:04.416250 -03:00 DEBUG starting service task {"local.ts":1732237684,"svc.version":"v0.1.0","svc.language":"rust","local.ts_ms":1732237684416,"task_name":"script","svc.name":"script-example","svc.product":"Matrix"}
120-
2024-11-21 22:08:04.416351 -03:00 INFO Start script service {"local.ts":1732237684,"svc.version":"v0.1.0","svc.language":"rust","local.ts_ms":1732237684416,"svc.name":"script-example","svc.product":"Matrix"}
121-
2024-11-21 22:08:04.416370 -03:00 DEBUG finishing service task {"local.ts":1732237684,"svc.version":"v0.1.0","svc.language":"rust","local.ts_ms":1732237684416,"task_name":"script","svc.name":"script-example","svc.product":"Matrix"}
122-
2024-11-21 22:08:04.416388 -03:00 DEBUG service task finished {"local.ts":1732237684,"svc.version":"v0.1.0","svc.language":"rust","local.ts_ms":1732237684416,"task_name":"script","svc.name":"script-example","svc.product":"Matrix"}
123-
lifecycle on_finish
124-
2024-11-21 22:08:04.416436 -03:00 INFO service stopped {"local.ts":1732237684,"svc.version":"v0.1.0","svc.language":"rust","local.ts_ms":1732237684416,"svc.name":"script-example","svc.product":"Matrix"}
112+
{"timestamp":"2024-12-04T07:09:02.173335-03:00","level":"INFO","message":"service starting","svc.name":"script-example","svc.version":"v0.1.0","svc.product":"Matrix","svc.language":"rust"}
113+
{"timestamp":"2024-12-04T07:09:02.173715-03:00","level":"INFO","message":"starting features","svc.name":"script-example","svc.version":"v0.1.0","svc.product":"Matrix","svc.language":"rust"}
114+
{"timestamp":"2024-12-04T07:09:02.173840-03:00","level":"INFO","message":"service resources","svc.name":"script-example","svc.version":"v0.1.0","svc.product":"Matrix","svc.language":"rust","example":{"test":"Hello world"}}
115+
{"timestamp":"2024-12-04T07:09:02.173919-03:00","level":"INFO","message":"service is running","svc.name":"script-example","svc.version":"v0.1.0","svc.product":"Matrix","svc.language":"rust","kind":"script"}
116+
{"timestamp":"2024-12-04T07:09:02.174001-03:00","level":"INFO","message":"Stop script service","svc.name":"script-example","svc.version":"v0.1.0","svc.product":"Matrix","svc.language":"rust"}
117+
{"timestamp":"2024-12-04T07:09:02.174056-03:00","level":"INFO","message":"Start script service","svc.name":"script-example","svc.version":"v0.1.0","svc.product":"Matrix","svc.language":"rust"}
118+
{"timestamp":"2024-12-04T07:09:02.174296-03:00","level":"INFO","message":"service stopped","svc.name":"script-example","svc.version":"v0.1.0","svc.product":"Matrix","svc.language":"rust"}
125119
```
126120

127121
For more examples, including how to create new features and new service kind,
128122
you can check the [examples](examples) directory.
129123

130124
## Roadmap
131125

132-
* Improve logger to use better structured log system and to set the log level
133-
using service definitions.
134126
* Enable features and services to use the same env system that the core uses.
135127
* Improve unit tests.
136128
* Full compatibility between go and rust gRPC services.

docs/mikros.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ as long as they have the same mode of execution (Block/NonBlock)
142142
Optional fields:
143143

144144
- envs: a string array of required environment variables for the service.
145+
- log: an object that allows defining the service initial settings for logging.
145146

146147
Additionally, you can use this same file for the following types of definitions:
147148

@@ -150,6 +151,8 @@ the application.
150151
- services: a map of service definitions, to set custom service kind settings
151152
for the application.
152153
- clients: a map of client connection definitions, for dependent applications.
154+
- service: a structure free service definition that the service can use for
155+
its own settings.
153156

154157
Example:
155158

@@ -161,15 +164,22 @@ language = "rust"
161164
product = "my-awesome-product"
162165
envs = [ "CUSTOM_ENV_1" ]
163166

167+
# Required settings for the simple_api feature
164168
[features.simple_api]
165169
enabled = true
166170

171+
# Custom settings for connecting with the 'grpc' gRPC client
167172
[clients.grpc]
168173
host = "localhost"
169174
port = 7071
170175

176+
# Required settings for the 'cronjob' service type
171177
[services.cronjob]
172178
frequency = "daily"
179+
180+
# Custom service settings
181+
[service]
182+
max_diff_range = 100
173183
```
174184

175185
### Environment variables

examples/apps/grpc/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7+
example = { path = "../../features/example" }
78
mikros = { path = "../../../" }
89
prost = "0.13.3"
910
tokio = { version = "1.41.1", features = ["full"] }

examples/apps/grpc/src/main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ pub mod helloworld {
44
include!("generated/helloworld.rs"); // Include generated code
55
}
66

7-
use mikros::features;
87
use mikros::service::{builder::ServiceBuilder, context};
98
use tonic::{Request, Response, Status};
109

@@ -33,7 +32,7 @@ impl Greeter for MyGreeter {
3332
ctx.logger()
3433
.info(format!("the inner value is: {}", self.ctx.lock().await.value).as_str());
3534

36-
let _ = features::example::execute_on(&ctx, |api| {
35+
let _ = example::execute_on(&ctx, |api| {
3736
api.do_something();
3837
Ok(())
3938
})

examples/apps/grpc_with_lifecycle/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7+
example = { path = "../../features/example" }
78
mikros = { path = "../../../" }
89
prost = "0.13.3"
910
tokio = { version = "1.41.1", features = ["full"] }

examples/apps/grpc_with_lifecycle/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pub mod helloworld {
55
}
66

77
use mikros::service::{builder::ServiceBuilder, context, lifecycle};
8-
use mikros::{features, link_grpc_service};
8+
use mikros::link_grpc_service;
99
use tonic::transport::Channel;
1010
use tonic::{Request, Response, Status};
1111

@@ -45,7 +45,7 @@ impl Greeter for MyGreeter {
4545
.say_hello(reply)
4646
.await?;
4747

48-
let _ = features::example::execute_on(&ctx, |api| {
48+
let _ = example::execute_on(&ctx, |api| {
4949
api.do_something();
5050
Ok(())
5151
})

examples/apps/native/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ edition = "2021"
55

66
[dependencies]
77
async-trait = "0.1.83"
8+
example = { path = "../../features/example" }
89
mikros = { path = "../../../" }
910
simple_api = { path = "../../features/simple_api" }
1011
tokio = { version = "1.41.1", features = ["full"] }

examples/apps/native/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ async fn main() {
88
let s = AppService::new();
99
let svc = ServiceBuilder::default()
1010
.native(Box::new(s))
11-
.with_features(simple_api::features())
11+
.with_features(vec![simple_api::new(), example::new()])
1212
.build();
1313

1414
match svc {

examples/apps/native/src/service.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use mikros::errors as merrors;
2-
use mikros::features::example;
32
use mikros::service::context::Context;
43

54
#[derive(Clone)]

examples/apps/script/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ edition = "2021"
55

66
[dependencies]
77
async-trait = "0.1.83"
8+
example = { path = "../../features/example" }
89
mikros = { path = "../../../" }
910
tokio = { version = "1.41.1", features = ["full"] }

examples/apps/script/service.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
name = "native-example"
1+
name = "script-example"
22
version = "v0.1.0"
33
language = "rust"
4-
product = "examples"
4+
product = "Matrix"
55
types = ["script"]

examples/apps/script/src/main.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ use service::Service as AppService;
66
#[tokio::main]
77
async fn main() {
88
let s = AppService::new();
9-
let svc = ServiceBuilder::default().script(Box::new(s)).build();
9+
let svc = ServiceBuilder::default()
10+
.script(Box::new(s))
11+
.with_features(vec![example::new()])
12+
.build();
1013

1114
match svc {
1215
Ok(mut svc) => svc.start().await,

examples/apps/script/src/service.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use mikros::errors as merrors;
2-
use mikros::features;
32
use mikros::service::context::Context;
43

54
#[derive(Clone)]
@@ -28,7 +27,7 @@ impl mikros::service::lifecycle::Lifecycle for Service {
2827
impl mikros::service::script::ScriptService for Service {
2928
async fn run(&self, ctx: &Context) -> merrors::Result<()> {
3029
ctx.logger().info("Start script service");
31-
features::example::execute_on(ctx, |api| {
30+
example::execute_on(ctx, |api| {
3231
api.do_something();
3332
Ok(())
3433
})

examples/features/example/src/lib.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
use std::any::Any;
2-
use std::collections::HashMap;
32
use std::sync::Arc;
43

5-
use logger::fields::FieldValue;
6-
74
use mikros::definition::Definitions;
85
use mikros::env::Env;
9-
use mikros::{errors as merrors, impl_feature_public_api};
10-
use mikros::{plugin, logger};
6+
use mikros::{errors as merrors, impl_feature_public_api, plugin, serde_json};
117
use mikros::service::context::Context;
128

139
/// The feature public API.
@@ -24,10 +20,10 @@ impl plugin::feature::Feature for Example {
2420
"example"
2521
}
2622

27-
fn info(&self) -> HashMap<String, FieldValue> {
28-
logger::fields![
29-
"test" => FieldValue::String("Hello world".to_string()),
30-
]
23+
fn info(&self) -> serde_json::Value {
24+
serde_json::json!({
25+
"test": "Hello world".to_string(),
26+
})
3127
}
3228

3329
fn is_enabled(&self) -> bool {

examples/features/simple_api/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ edition = "2021"
66
[dependencies]
77
async-trait = "0.1.83"
88
mikros = { path = "../../../" }
9-
serde = "1.0.215"
9+
serde = "1.0.215"
10+
serde_derive = "1.0.215"

examples/features/simple_api/src/lib.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
use std::any::Any;
2-
use std::collections::HashMap;
32
use std::sync::Arc;
43

54
use mikros::env::Env;
6-
use mikros::{errors as merrors, impl_feature_public_api};
7-
use mikros::logger;
5+
use mikros::{errors as merrors, impl_feature_public_api, serde_json};
86
use mikros::plugin;
97
use mikros::service::context::Context;
10-
use serde::Deserialize;
8+
use serde_derive::Deserialize;
119

1210
/// The feature public API.
1311
pub trait ExampleAPI {
@@ -60,12 +58,12 @@ impl plugin::feature::Feature for Example {
6058
Some(self)
6159
}
6260

63-
fn info(&self) -> HashMap<String, logger::fields::FieldValue> {
61+
fn info(&self) -> serde_json::Value {
6462
let collections = self.definitions.collections.join(",");
65-
logger::fields![
66-
"test" => logger::fields::FieldValue::String("Hello world".to_string()),
67-
"collections" => logger::fields::FieldValue::String(collections),
68-
]
63+
serde_json::json!({
64+
"test": "Hello world".to_string(),
65+
"collections": collections,
66+
})
6967
}
7068

7169
async fn cleanup(&self) {

examples/services/cronjob/src/lib.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
pub mod builder;
22

3-
use std::collections::HashMap;
43
use std::sync::Arc;
54

65
use mikros::definition::ServiceKind;
76
use mikros::env::Env;
8-
use mikros::logger::fields::FieldValue;
9-
use mikros::{errors as merrors, plugin, Mutex};
7+
use mikros::{errors as merrors, plugin, Mutex, serde_json};
108
use mikros::service::context::Context;
119
use mikros::service::lifecycle::Lifecycle;
1210
use serde_derive::Deserialize;
@@ -46,8 +44,15 @@ impl plugin::service::Service for Cronjob {
4644
ServiceKind::Custom("cronjob".into())
4745
}
4846

49-
fn info(&self) -> HashMap<String, FieldValue> {
50-
HashMap::new()
47+
fn info(&self) -> serde_json::Value {
48+
match self.definitions {
49+
Some(ref d) => serde_json::json!({
50+
"frequency": d.frequency,
51+
"days": d.days,
52+
"scheduled_times": d.scheduled_times,
53+
}),
54+
None => serde_json::json!({}),
55+
}
5156
}
5257

5358
fn initialize(&mut self, _envs: Arc<Env>, definitions: Arc<mikros::definition::Definitions>) -> merrors::Result<()> {

src/definition/mod.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,19 @@ pub struct Definitions {
3333
pub types: Vec<service::Service>,
3434
}
3535

36-
#[derive(serde_derive::Deserialize, Debug)]
36+
#[derive(serde_derive::Deserialize, Debug, Clone)]
3737
pub struct Log {
38-
pub level: String
38+
pub level: String,
39+
pub local_timestamp: bool,
40+
}
41+
42+
impl Default for Log {
43+
fn default() -> Self {
44+
Log {
45+
level: "info".to_string(),
46+
local_timestamp: true,
47+
}
48+
}
3949
}
4050

4151
#[derive(serde_derive::Deserialize, Debug, Clone)]
@@ -133,6 +143,13 @@ impl Definitions {
133143
}
134144
}
135145

146+
pub(crate) fn log(&self) -> Log {
147+
match &self.log {
148+
Some(l) => l.clone(),
149+
None => Log::default()
150+
}
151+
}
152+
136153
/// Loads definitions from a feature.
137154
pub fn load_feature<T>(&self, feature: &str) -> merrors::Result<Option<T>>
138155
where

src/definition/service.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::fmt::Formatter;
2+
23
use serde::de::{Deserializer, Visitor, Error as SerdeError};
34
use serde::{Deserialize, de::IntoDeserializer};
45

@@ -7,7 +8,7 @@ use crate::definition::ServiceKind;
78
#[derive(Debug, Clone)]
89
pub struct Service(pub ServiceKind, pub Option<i32>);
910

10-
impl<'a> serde::Deserialize<'a> for Service {
11+
impl<'a> Deserialize<'a> for Service {
1112
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
1213
where
1314
D: Deserializer<'a>
@@ -66,7 +67,7 @@ mod tests {
6667
use super::*;
6768

6869
// Wrapper struct to deserialize from TOML
69-
#[derive(Debug, Deserialize)]
70+
#[derive(Debug, serde_derive::Deserialize)]
7071
struct Config {
7172
#[serde(deserialize_with = "deserialize_services")]
7273
types: Vec<Service>,

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ pub mod definition;
22
pub mod env;
33
pub mod errors;
44
pub mod http;
5+
pub mod logger;
56
pub mod plugin;
67
pub mod service;
78

@@ -10,5 +11,5 @@ mod grpc;
1011

1112
// Forward some declarations for services
1213
pub use futures::lock::Mutex;
13-
pub use logger;
1414
pub use tokio::sync::watch;
15+
pub use serde_json;

0 commit comments

Comments
 (0)