Struct afire::extension::Logger

source ยท
pub struct Logger { /* private fields */ }
Expand description

Log requests to the console or a file.

Implementationsยง

sourceยง

impl Logger

source

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();
    }
source

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();
    }
source

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();
    }
source

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();
    }
source

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 Debug for Logger

sourceยง

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
sourceยง

impl Default for Logger

sourceยง

fn default() -> Logger

Returns the โ€œdefault valueโ€ for a type. Read more
sourceยง

impl Middleware for Logger

sourceยง

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

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

Middleware to run Before Routes
sourceยง

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

Middleware to run After Routes
sourceยง

fn end_raw(&self, req: &Result<Request>, res: &Result<Response>)

Middleware to run after the response has been handled. Because this is the raw version of Middleware::end, it is passed a Result. The default implementation calls Middleware::end if the Result is Ok.
sourceยง

fn attach<State>(self, server: &mut Server<State>)where Self: 'static + Send + Sync + Sized, State: 'static + Send + Sync,

Attach Middleware to a Server. If you want to get a reference to the serverโ€™s state in your middleware state, you should override this method.

Auto Trait Implementationsยง

Blanket Implementationsยง

sourceยง

impl<T> Any for Twhere T: 'static + ?Sized,

sourceยง

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
sourceยง

impl<T> Borrow<T> for Twhere T: ?Sized,

sourceยง

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
sourceยง

impl<T> BorrowMut<T> for Twhere T: ?Sized,

sourceยง

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
sourceยง

impl<T> From<T> for T

sourceยง

fn from(t: T) -> T

Returns the argument unchanged.

sourceยง

impl<T, U> Into<U> for Twhere U: From<T>,

sourceยง

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

sourceยง

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

ยง

type Error = Infallible

The type returned in the event of a conversion error.
sourceยง

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
sourceยง

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

ยง

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
sourceยง

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.