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
32dd9766
Commit
32dd9766
authored
Sep 08, 2016
by
astaxie
Committed by
GitHub
Sep 08, 2016
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2146 from philchia/develop
Fix the typo
parents
6ced2666
fcd8a202
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
147 additions
and
1 deletion
+147
-1
jianliao.go
logs/jianliao.go
+78
-0
log.go
logs/log.go
+3
-1
slack.go
logs/slack.go
+66
-0
No files found.
logs/jianliao.go
0 → 100644
View file @
32dd9766
package
logs
import
(
"encoding/json"
"fmt"
"net/http"
"net/url"
"time"
)
// JLWriter implements beego LoggerInterface and is used to send jiaoliao webhook
type
JLWriter
struct
{
AuthorName
string
`json:"authorname"`
Title
string
`json:"title"`
WebhookURL
string
`json:"webhookurl"`
RedirectURL
string
`json:"redirecturl,omitempty"`
ImageURL
string
`json:"imageurl,omitempty"`
Level
int
`json:"level"`
}
// newJLWriter create jiaoliao writer.
func
newJLWriter
()
Logger
{
return
&
JLWriter
{
Level
:
LevelTrace
}
}
// Init JLWriter with json config string
func
(
s
*
JLWriter
)
Init
(
jsonconfig
string
)
error
{
err
:=
json
.
Unmarshal
([]
byte
(
jsonconfig
),
s
)
if
err
!=
nil
{
return
err
}
return
nil
}
// WriteMsg write message in smtp writer.
// it will send an email with subject and only this message.
func
(
s
*
JLWriter
)
WriteMsg
(
when
time
.
Time
,
msg
string
,
level
int
)
error
{
if
level
>
s
.
Level
{
return
nil
}
text
:=
fmt
.
Sprintf
(
"%s %s"
,
when
.
Format
(
"2006-01-02 15:04:05"
),
msg
)
form
:=
url
.
Values
{}
form
.
Add
(
"authorName"
,
s
.
AuthorName
)
form
.
Add
(
"title"
,
s
.
Title
)
form
.
Add
(
"text"
,
text
)
if
s
.
RedirectURL
!=
""
{
form
.
Add
(
"redirectUrl"
,
s
.
RedirectURL
)
}
if
s
.
ImageURL
!=
""
{
form
.
Add
(
"imageUrl"
,
s
.
ImageURL
)
}
resp
,
err
:=
http
.
PostForm
(
s
.
WebhookURL
,
form
)
if
err
!=
nil
{
return
err
}
if
resp
.
StatusCode
!=
http
.
StatusOK
{
return
fmt
.
Errorf
(
"Post webhook failed %s %d"
,
resp
.
Status
,
resp
.
StatusCode
)
}
resp
.
Body
.
Close
()
return
nil
}
// Flush implementing method. empty.
func
(
s
*
JLWriter
)
Flush
()
{
return
}
// Destroy implementing method. empty.
func
(
s
*
JLWriter
)
Destroy
()
{
return
}
func
init
()
{
Register
(
AdapterJianLiao
,
newJLWriter
)
}
logs/log.go
View file @
32dd9766
...
...
@@ -66,9 +66,11 @@ const (
AdapterConsole
=
"console"
AdapterFile
=
"file"
AdapterMultiFile
=
"multifile"
AdapterMail
=
"s
tm
p"
AdapterMail
=
"s
mt
p"
AdapterConn
=
"conn"
AdapterEs
=
"es"
AdapterJianLiao
=
"jianliao"
AdapterSlack
=
"slack"
)
// Legacy log level constants to ensure backwards compatibility.
...
...
logs/slack.go
0 → 100644
View file @
32dd9766
package
logs
import
(
"encoding/json"
"fmt"
"net/http"
"net/url"
"time"
)
// SLACKWriter implements beego LoggerInterface and is used to send jiaoliao webhook
type
SLACKWriter
struct
{
WebhookURL
string
`json:"webhookurl"`
Level
int
`json:"level"`
}
// newSLACKWriter create jiaoliao writer.
func
newSLACKWriter
()
Logger
{
return
&
SLACKWriter
{
Level
:
LevelTrace
}
}
// Init SLACKWriter with json config string
func
(
s
*
SLACKWriter
)
Init
(
jsonconfig
string
)
error
{
err
:=
json
.
Unmarshal
([]
byte
(
jsonconfig
),
s
)
if
err
!=
nil
{
return
err
}
return
nil
}
// WriteMsg write message in smtp writer.
// it will send an email with subject and only this message.
func
(
s
*
SLACKWriter
)
WriteMsg
(
when
time
.
Time
,
msg
string
,
level
int
)
error
{
if
level
>
s
.
Level
{
return
nil
}
text
:=
fmt
.
Sprintf
(
"{
\"
text
\"
:
\"
%s %s
\"
}"
,
when
.
Format
(
"2006-01-02 15:04:05"
),
msg
)
form
:=
url
.
Values
{}
form
.
Add
(
"payload"
,
text
)
resp
,
err
:=
http
.
PostForm
(
s
.
WebhookURL
,
form
)
if
err
!=
nil
{
return
err
}
if
resp
.
StatusCode
!=
http
.
StatusOK
{
return
fmt
.
Errorf
(
"Post webhook failed %s %d"
,
resp
.
Status
,
resp
.
StatusCode
)
}
resp
.
Body
.
Close
()
return
nil
}
// Flush implementing method. empty.
func
(
s
*
SLACKWriter
)
Flush
()
{
return
}
// Destroy implementing method. empty.
func
(
s
*
SLACKWriter
)
Destroy
()
{
return
}
func
init
()
{
Register
(
AdapterSlack
,
newSLACKWriter
)
}
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