pub struct Logger { /* private fields */ }
Expand description
Log requests to the console or a file.
Implementationsยง
sourceยงimpl Logger
impl Logger
sourcepub fn new() -> Logger
pub fn new() -> Logger
Make a new logger
The default settings are as follows
-
Log Level:
Level::Info
-
File:
None
-
Console:
true
Example
// Import Lib
use afire::extension::logger::{Logger, Level};
// Create a new logger
let logger = Logger::new();
Examples found in repository?
examples/basic/logging.rs (line 34)
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
fn exec(&self) {
// Create a new Server instance on localhost port 8080
let mut server = Server::<()>::new("localhost", 8080);
// Define a handler for GET "/"
server.route(Method::GET, "/", |_req| {
Response::new()
.text("Hello World!\nThis request has been logged!")
.content(Content::TXT)
});
// Make a logger and attach it to the server
// By default Log Level is INFO, File is None and Console is true
// This could be condensed to `Logger::new().attach(&mut server);` as it uses al default values
Logger::new()
// The level of logging this can be Debug or Info
// Debug will give a lot more information about the request
.level(Level::Info)
// This will have Logger make use of the RealIp extention,
// which will allow logging the correct IP when using a reverse proxy.
.real_ip(HeaderType::XForwardedFor)
// The file argument tells the logger if it should save to a file
// Only one file can be defined per logger
// With logging to file it will write to the file on every request... (for now)
.file("example.log")
.unwrap()
// Tells the Logger it should log to the console as well
.console(true)
// This must be put at the end of your Logger Construction
// It adds the Logger to your Server as Middleware
.attach(&mut server);
// Now if you goto http://localhost:8080/ you should see the log message in console.
// Start the server
// This will block the current thread
server.start().unwrap();
}
sourcepub fn level(self, level: Level) -> Self
pub fn level(self, level: Level) -> Self
Set the log Level of a logger
Example
// Import Lib
use afire::extension::logger::{Logger, Level};
// Create a new logger
let logger = Logger::new()
.level(Level::Debug);
Examples found in repository?
examples/basic/logging.rs (line 37)
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
fn exec(&self) {
// Create a new Server instance on localhost port 8080
let mut server = Server::<()>::new("localhost", 8080);
// Define a handler for GET "/"
server.route(Method::GET, "/", |_req| {
Response::new()
.text("Hello World!\nThis request has been logged!")
.content(Content::TXT)
});
// Make a logger and attach it to the server
// By default Log Level is INFO, File is None and Console is true
// This could be condensed to `Logger::new().attach(&mut server);` as it uses al default values
Logger::new()
// The level of logging this can be Debug or Info
// Debug will give a lot more information about the request
.level(Level::Info)
// This will have Logger make use of the RealIp extention,
// which will allow logging the correct IP when using a reverse proxy.
.real_ip(HeaderType::XForwardedFor)
// The file argument tells the logger if it should save to a file
// Only one file can be defined per logger
// With logging to file it will write to the file on every request... (for now)
.file("example.log")
.unwrap()
// Tells the Logger it should log to the console as well
.console(true)
// This must be put at the end of your Logger Construction
// It adds the Logger to your Server as Middleware
.attach(&mut server);
// Now if you goto http://localhost:8080/ you should see the log message in console.
// Start the server
// This will block the current thread
server.start().unwrap();
}
sourcepub fn real_ip(self, real_ip: HeaderType) -> Self
pub fn real_ip(self, real_ip: HeaderType) -> Self
Uses the crate::extension::RealIp
extension for log IPs.
You will need to supply the header that will contain the IP address, for example the X-Forwarded-For header (HeaderType::XForwardedFor
)
Warning: Make sure your reverse proxy is overwriting the specified header on the incoming requests so clients cant spoof their original Ips.
Examples found in repository?
examples/basic/logging.rs (line 40)
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
fn exec(&self) {
// Create a new Server instance on localhost port 8080
let mut server = Server::<()>::new("localhost", 8080);
// Define a handler for GET "/"
server.route(Method::GET, "/", |_req| {
Response::new()
.text("Hello World!\nThis request has been logged!")
.content(Content::TXT)
});
// Make a logger and attach it to the server
// By default Log Level is INFO, File is None and Console is true
// This could be condensed to `Logger::new().attach(&mut server);` as it uses al default values
Logger::new()
// The level of logging this can be Debug or Info
// Debug will give a lot more information about the request
.level(Level::Info)
// This will have Logger make use of the RealIp extention,
// which will allow logging the correct IP when using a reverse proxy.
.real_ip(HeaderType::XForwardedFor)
// The file argument tells the logger if it should save to a file
// Only one file can be defined per logger
// With logging to file it will write to the file on every request... (for now)
.file("example.log")
.unwrap()
// Tells the Logger it should log to the console as well
.console(true)
// This must be put at the end of your Logger Construction
// It adds the Logger to your Server as Middleware
.attach(&mut server);
// Now if you goto http://localhost:8080/ you should see the log message in console.
// Start the server
// This will block the current thread
server.start().unwrap();
}
sourcepub fn file(self, file: impl AsRef<Path>) -> Result<Self>
pub fn file(self, file: impl AsRef<Path>) -> Result<Self>
Set the log file of a logger
Example
// Import Lib
use afire::extension::logger::{Logger, Level};
// Create a new logger and enable logging to file
let logger = Logger::new()
.file("nose.txt");
Examples found in repository?
examples/basic/logging.rs (line 44)
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
fn exec(&self) {
// Create a new Server instance on localhost port 8080
let mut server = Server::<()>::new("localhost", 8080);
// Define a handler for GET "/"
server.route(Method::GET, "/", |_req| {
Response::new()
.text("Hello World!\nThis request has been logged!")
.content(Content::TXT)
});
// Make a logger and attach it to the server
// By default Log Level is INFO, File is None and Console is true
// This could be condensed to `Logger::new().attach(&mut server);` as it uses al default values
Logger::new()
// The level of logging this can be Debug or Info
// Debug will give a lot more information about the request
.level(Level::Info)
// This will have Logger make use of the RealIp extention,
// which will allow logging the correct IP when using a reverse proxy.
.real_ip(HeaderType::XForwardedFor)
// The file argument tells the logger if it should save to a file
// Only one file can be defined per logger
// With logging to file it will write to the file on every request... (for now)
.file("example.log")
.unwrap()
// Tells the Logger it should log to the console as well
.console(true)
// This must be put at the end of your Logger Construction
// It adds the Logger to your Server as Middleware
.attach(&mut server);
// Now if you goto http://localhost:8080/ you should see the log message in console.
// Start the server
// This will block the current thread
server.start().unwrap();
}
sourcepub fn console(self, console: bool) -> Self
pub fn console(self, console: bool) -> Self
Enable writing events to stdout
Example
// Import Lib
use afire::extension::logger::{Logger, Level};
// Create a new logger and enable console
let logger = Logger::new()
.console(true );
Examples found in repository?
examples/basic/logging.rs (line 47)
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
fn exec(&self) {
// Create a new Server instance on localhost port 8080
let mut server = Server::<()>::new("localhost", 8080);
// Define a handler for GET "/"
server.route(Method::GET, "/", |_req| {
Response::new()
.text("Hello World!\nThis request has been logged!")
.content(Content::TXT)
});
// Make a logger and attach it to the server
// By default Log Level is INFO, File is None and Console is true
// This could be condensed to `Logger::new().attach(&mut server);` as it uses al default values
Logger::new()
// The level of logging this can be Debug or Info
// Debug will give a lot more information about the request
.level(Level::Info)
// This will have Logger make use of the RealIp extention,
// which will allow logging the correct IP when using a reverse proxy.
.real_ip(HeaderType::XForwardedFor)
// The file argument tells the logger if it should save to a file
// Only one file can be defined per logger
// With logging to file it will write to the file on every request... (for now)
.file("example.log")
.unwrap()
// Tells the Logger it should log to the console as well
.console(true)
// This must be put at the end of your Logger Construction
// It adds the Logger to your Server as Middleware
.attach(&mut server);
// Now if you goto http://localhost:8080/ you should see the log message in console.
// Start the server
// This will block the current thread
server.start().unwrap();
}
Trait Implementationsยง
sourceยงimpl Middleware for Logger
impl Middleware for Logger
sourceยงfn end(&self, req: &Request, _res: &Response)
fn end(&self, req: &Request, _res: &Response)
Middleware ot run after the response has been handled
sourceยงfn pre_raw(&self, req: &mut Result<Request>) -> MiddleResult
fn pre_raw(&self, req: &mut Result<Request>) -> MiddleResult
Middleware to run before routes.
Because this is the
raw
version of Middleware::pre
, it is passed a Result
.
The default implementation calls Middleware::pre
if the Result
is Ok
.sourceยงfn pre(&self, _req: &mut Request) -> MiddleResult
fn pre(&self, _req: &mut Request) -> MiddleResult
Middleware to run Before Routes
sourceยงfn post_raw(
&self,
req: Result<Rc<Request>>,
res: &mut Result<Response>
) -> MiddleResult
fn post_raw( &self, req: Result<Rc<Request>>, res: &mut Result<Response> ) -> MiddleResult
Middleware to run after routes.
Because this is the
raw
version of Middleware::post
, it is passed a Result
.
The default implementation calls Middleware::post
if the Result
is Ok
.sourceยงfn post(&self, _req: &Request, _res: &mut Response) -> MiddleResult
fn post(&self, _req: &Request, _res: &mut Response) -> MiddleResult
Middleware to run After Routes
Auto Trait Implementationsยง
impl RefUnwindSafe for Logger
impl Send for Logger
impl Sync for Logger
impl Unpin for Logger
impl UnwindSafe for Logger
Blanket Implementationsยง
sourceยงimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
sourceยงfn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more