axslogparser.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package axslogparser
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. )
  7. // Parser is the interface for accesslog
  8. type Parser interface {
  9. Parse(string) (*Log, error)
  10. }
  11. // Log is the struct stored parsed result of single line of accesslog
  12. type Log struct {
  13. VirtualHost string `ltsv:"vhost"`
  14. Host string
  15. RemoteLogname string `ltsv:"ident"`
  16. User string
  17. Time time.Time `ltsv:"-"`
  18. TimeStr string `ltsv:"time"`
  19. Request string `ltsv:"req"`
  20. Status int
  21. Size uint64
  22. Referer string
  23. UserAgent string `ltsv:"ua"`
  24. ReqTime *float64
  25. ReqTimeMicroSec *float64 `ltsv:"reqtime_microsec"`
  26. AppTime *float64
  27. TakenSec *float64 `ltsv:"taken_sec"` // Hatena specific
  28. ForwardedFor string
  29. RequestURI string `ltsv:"uri"`
  30. Protocol string
  31. Method string
  32. }
  33. func (l *Log) breakdownRequest() error {
  34. if l.RequestURI != "" && l.Protocol != "" && l.Method != "" {
  35. return nil
  36. }
  37. stuff := strings.Fields(l.Request)
  38. if len(stuff) != 3 {
  39. return fmt.Errorf("invalid request: %s", l.Request)
  40. }
  41. if len(stuff) > 0 && l.Method == "" {
  42. l.Method = stuff[0]
  43. }
  44. if len(stuff) > 1 && l.RequestURI == "" {
  45. l.RequestURI = stuff[1]
  46. }
  47. if len(stuff) > 2 && l.Protocol == "" {
  48. l.Protocol = stuff[2]
  49. }
  50. return nil
  51. }
  52. const clfTimeLayout = "02/Jan/2006:15:04:05 -0700"
  53. // GuessParser guesses the parser from line
  54. func GuessParser(line string) (Parser, *Log, error) {
  55. var p Parser
  56. if strings.Contains(line, "\thost:") || strings.Contains(line, "\ttime:") {
  57. p = &LTSV{}
  58. l, err := p.Parse(line)
  59. if err == nil {
  60. return p, l, err
  61. }
  62. }
  63. p = &Apache{}
  64. l, err := p.Parse(line)
  65. if err != nil {
  66. return nil, nil, err
  67. }
  68. return p, l, nil
  69. }
  70. // Parse log line
  71. func Parse(line string) (*Log, error) {
  72. _, l, err := GuessParser(line)
  73. return l, err
  74. }