The class provides a default set of logging functions for the various stages of the request handled by a DNSServer instance which are enabled/disabled by flags in the 'log' class variable. To customise logging create an object which implements the DNSLogger
| 173 | return rdata |
| 174 | |
| 175 | class DNSLogger: |
| 176 | |
| 177 | """ |
| 178 | The class provides a default set of logging functions for the various |
| 179 | stages of the request handled by a DNSServer instance which are |
| 180 | enabled/disabled by flags in the 'log' class variable. |
| 181 | |
| 182 | To customise logging create an object which implements the DNSLogger |
| 183 | interface and pass instance to DNSServer. |
| 184 | |
| 185 | The methods which the logger instance must implement are: |
| 186 | |
| 187 | log_recv - Raw packet received |
| 188 | log_send - Raw packet sent |
| 189 | log_request - DNS Request |
| 190 | log_reply - DNS Response |
| 191 | log_truncated - Truncated |
| 192 | log_error - Decoding error |
| 193 | log_data - Dump full request/response |
| 194 | """ |
| 195 | |
| 196 | def __init__(self,log="",prefix=True): |
| 197 | """ |
| 198 | Selectively enable log hooks depending on log argument |
| 199 | (comma separated list of hooks to enable/disable) |
| 200 | |
| 201 | - If empty enable default log hooks |
| 202 | - If entry starts with '+' (eg. +send,+recv) enable hook |
| 203 | - If entry starts with '-' (eg. -data) disable hook |
| 204 | - If entry doesn't start with +/- replace defaults |
| 205 | |
| 206 | Prefix argument enables/disables log prefix |
| 207 | """ |
| 208 | default = ["request","reply","truncated","error"] |
| 209 | log = log.split(",") if log else [] |
| 210 | enabled = set([ s for s in log if s[0] not in '+-'] or default) |
| 211 | [ enabled.add(l[1:]) for l in log if l.startswith('+') ] |
| 212 | [ enabled.discard(l[1:]) for l in log if l.startswith('-') ] |
| 213 | for l in ['log_recv','log_send','log_request','log_reply', |
| 214 | 'log_truncated','log_error','log_data']: |
| 215 | if l[4:] not in enabled: |
| 216 | setattr(self,l,self.log_pass) |
| 217 | self.prefix = prefix |
| 218 | |
| 219 | def log_pass(self,*args): |
| 220 | pass |
| 221 | |
| 222 | def log_prefix(self,handler): |
| 223 | if self.prefix: |
| 224 | return "%s [%s:%s] " % (time.strftime("%Y-%m-%d %X"), |
| 225 | handler.__class__.__name__, |
| 226 | handler.server.resolver.__class__.__name__) |
| 227 | else: |
| 228 | return "" |
| 229 | |
| 230 | def log_recv(self,handler,data): |
| 231 | print("%sReceived: [%s:%d] (%s) <%d> : %s" % ( |
| 232 | self.log_prefix(handler), |
no outgoing calls
no test coverage detected