Skip to content

Commit

Permalink
Adding support to specify which field to print (#2)
Browse files Browse the repository at this point in the history
* docs: removing license header in the cmd files(cat,list,root)

* feat: Adding support choose whiche field that should be printed from the accesslog
  • Loading branch information
dbgeek authored Mar 2, 2019
1 parent a639877 commit 674f76a
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 59 deletions.
25 changes: 6 additions & 19 deletions cmd/cat.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
// Copyright © 2019 NAME HERE <EMAIL ADDRESS>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
Expand Down Expand Up @@ -65,12 +51,11 @@ possible user these filter
c := logcat.NewRowFilter()
b := bytes.NewBuffer(buff.Bytes())
a := logcat.Accesslog{
Content: b,
RowFilter: c,
}
for _, row := range a.Cat() {
fmt.Println(row)
Content: b,
RowFilter: c,
PrintFields: viper.GetString("fields"),
}
a.Cat()
}
},
}
Expand All @@ -95,4 +80,6 @@ func init() {
viper.BindPFlag("target-status-code", catCmd.PersistentFlags().Lookup("target-status-code"))
catCmd.PersistentFlags().StringP("http-method", "", ".*", "")
viper.BindPFlag("http-method", catCmd.PersistentFlags().Lookup("http-method"))
catCmd.PersistentFlags().StringP("fields", "", "type timestamp elb client:port", "field to print")
viper.BindPFlag("fields", catCmd.PersistentFlags().Lookup("fields"))
}
14 changes: 0 additions & 14 deletions cmd/list.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
// Copyright © 2019 NAME HERE <EMAIL ADDRESS>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
Expand Down
20 changes: 0 additions & 20 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,3 @@
// Copyright © 2019 Björn Ahl
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package cmd

import (
Expand Down
70 changes: 64 additions & 6 deletions logcat/cat.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@ import (
"bufio"
"bytes"
"compress/gzip"
"encoding/csv"
"fmt"
"os"
"regexp"
"strings"
"text/tabwriter"

"github.com/dbgeek/elblogcat/logworker"
"github.com/spf13/viper"
)

type (
Accesslog struct {
Content *bytes.Buffer
RowFilter Filter
Content *bytes.Buffer
RowFilter Filter
PrintFields string
}
Filter struct {
ClientIP string
Expand All @@ -30,22 +35,75 @@ type (
matchString string
matcher *regexp.Regexp
}
accessLogFieldPosition map[string]int
)

func (a *Accesslog) Cat() []string {
var (
fieldPosition = map[string]int{
"conn-type": 0,
"timestamp": 1,
"elb": 2,
"client:port": 3,
"target:port": 4,
"request_porecessing_time": 5,
"target_processing_time": 6,
"respsone_processing_time": 7,
"elbStatis_code": 8,
"targetStatus_code": 9,
"received_bytes": 10,
"send_bytes": 11,
"request": 12,
"user-agent": 13,
"ssl-cipher": 14,
"ssl-protocol": 15,
"target-group-arn": 16,
"trace_id": 17,
"domain_name": 18,
"chose_cert_arn": 19,
"marched_rule_priority": 20,
"request_creation_time": 21,
"action_executed": 22,
"redirect_url": 23,
"error_reason": 24,
}
)

func (a *Accesslog) Cat() {
//ToDO Break out this to it own method on the acceslog
var pFields []int
for _, v := range strings.Fields(a.PrintFields) {
pFields = append(pFields, fieldPosition[v])
}

gzReader, err := gzip.NewReader(a.Content)
if err != nil {
logworker.Logger.Fatalf("new gzip reader failed with: %v", err)
}
s := []string{}
scanner := bufio.NewScanner(gzReader)
filter := newRowMatch(a.RowFilter)
var entries [][]string
for scanner.Scan() {
if filter.matcher.MatchString(scanner.Text()) {
s = append(s, scanner.Text())
//This should be break out to it´s own function
r := csv.NewReader(strings.NewReader(scanner.Text()))
r.Comma = ' '
field, err := r.Read()
if err != nil {
fmt.Println(err)
}
entries = append(entries, field)
}
}
tw := new(tabwriter.Writer)
tw.Init(os.Stdout, 0, 8, 2, '\t', 0)
defer tw.Flush()
for _, v := range entries {
var str string
for _, val := range pFields {
str += fmt.Sprintf("%s\t", v[val])
}
fmt.Fprintln(tw, str)
}
return s

}
func newRowMatch(filter Filter) *rowMatch {
Expand Down

0 comments on commit 674f76a

Please sign in to comment.