0001    // LogMiddleware.swift
0002    //
0003    // The MIT License (MIT)
0004    //
0005    // Copyright (c) 2015 Zewo
0006    //
0007    // Permission is hereby granted, free of charge, to any person obtaining a copy
0008    // of this software and associated documentation files (the "Software"), to deal
0009    // in the Software without restriction, including without limitation the rights
0010    // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
0011    // copies of the Software, and to permit persons to whom the Software is
0012    // furnished to do so, subject to the following conditions:
0013    //
0014    // The above copyright notice and this permission notice shall be included in all
0015    // copies or substantial portions of the Software.
0016    //
0017    // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0018    // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0019    // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0020    // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0021    // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
0022    // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
0023    // SOFTWARE.
0024    
0025    @_exported import Log
0026    @_exported import HTTP
0027    
0028    public struct LogMiddleware: MiddlewareType {
0029        private let log: Log
0030        private let level: Log.Level
0031    
0032        public init(log: Log, level: Log.Level = .Info) {
0033            self.log = log
0034            self.level = level
0035        }
0036    
0037        public func respond(request: Request, chain: ChainType) throws -> Response {
0038            let response = try chain.proceed(request)
0039            var message = "================================================================================\n"
0040            message += "Request:\n"
0041            message += "\(request)\n"
0042            message += "--------------------------------------------------------------------------------\n"
0043            message += "Response:\n"
0044            message += "\(response)\n"
0045            message += "================================================================================\n\n"
0046            log.log(level, item: message)
0047            return response
0048        }
0049    }
0050    
0051    public struct DebugLogMiddleware: MiddlewareType {
0052        private let log: Log
0053        private let level: Log.Level
0054    
0055        public init(log: Log, level: Log.Level = .Info) {
0056            self.log = log
0057            self.level = level
0058        }
0059    
0060        public func respond(request: Request, chain: ChainType) throws -> Response {
0061            let response = try chain.proceed(request)
0062            var message = "================================================================================\n"
0063            message += "Request:\n"
0064            message += "\(request.debugDescription)\n"
0065            message += "--------------------------------------------------------------------------------\n"
0066            message += "Response:\n"
0067            message += "\(response.debugDescription)\n"
0068            message += "================================================================================\n\n"
0069            log.log(level, item: message)
0070            return response
0071        }
0072    }