Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
beego
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
beego
Commits
d9bb1a35
Commit
d9bb1a35
authored
Jun 12, 2015
by
astaxie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
logs support elasticsearch adapter
parent
9c6775c2
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
76 additions
and
0 deletions
+76
-0
es.go
logs/es/es.go
+76
-0
No files found.
logs/es/es.go
0 → 100644
View file @
d9bb1a35
package
es
import
(
"encoding/json"
"errors"
"fmt"
"net"
"net/url"
"time"
"github.com/astaxie/beego/logs"
"github.com/belogik/goes"
)
func
NewES
()
logs
.
LoggerInterface
{
cw
:=
&
esLogger
{
Level
:
logs
.
LevelDebug
,
}
return
cw
}
type
esLogger
struct
{
*
goes
.
Connection
DSN
string
`json:"dsn"`
Level
int
`json:"level"`
}
// {"dsn":"http://localhost:9200/","level":1}
func
(
el
*
esLogger
)
Init
(
jsonconfig
string
)
error
{
err
:=
json
.
Unmarshal
([]
byte
(
jsonconfig
),
el
)
if
err
!=
nil
{
return
err
}
if
el
.
DSN
==
""
{
return
errors
.
New
(
"empty dsn"
)
}
else
if
u
,
err
:=
url
.
Parse
(
el
.
DSN
);
err
!=
nil
{
return
err
}
else
if
u
.
Path
==
""
{
return
errors
.
New
(
"missing prefix"
)
}
else
if
host
,
port
,
err
:=
net
.
SplitHostPort
(
u
.
Host
);
err
!=
nil
{
return
err
}
else
{
conn
:=
goes
.
NewConnection
(
host
,
port
)
el
.
Connection
=
conn
}
return
nil
}
func
(
el
*
esLogger
)
WriteMsg
(
msg
string
,
level
int
)
error
{
if
level
>
el
.
Level
{
return
nil
}
t
:=
time
.
Now
()
vals
:=
make
(
map
[
string
]
interface
{})
vals
[
"@timestamp"
]
=
t
.
Format
(
time
.
RFC3339
)
vals
[
"@msg"
]
=
msg
d
:=
goes
.
Document
{
Index
:
fmt
.
Sprintf
(
"%04d.%02d.%02d"
,
t
.
Year
(),
t
.
Month
(),
t
.
Day
()),
Type
:
"logs"
,
Fields
:
vals
,
}
_
,
err
:=
el
.
Index
(
d
,
nil
)
return
err
}
func
(
el
*
esLogger
)
Destroy
()
{
}
func
(
el
*
esLogger
)
Flush
()
{
}
func
init
()
{
logs
.
Register
(
"es"
,
NewES
)
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment