You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by de...@apache.org on 2018/02/28 16:57:35 UTC

[incubator-trafficcontrol] 01/07: add Logger interface

This is an automated email from the ASF dual-hosted git repository.

dewrich pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-trafficcontrol.git

commit 4fc976c2459c55d4df1a9c4d6df6979201b7f05e
Author: Dan Kirkwood <da...@apache.org>
AuthorDate: Fri Feb 23 13:52:17 2018 -0700

    add Logger interface
---
 traffic_ops/traffic_ops_golang/api/change_log.go | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/traffic_ops/traffic_ops_golang/api/change_log.go b/traffic_ops/traffic_ops_golang/api/change_log.go
index ac47809..b51b60b 100644
--- a/traffic_ops/traffic_ops_golang/api/change_log.go
+++ b/traffic_ops/traffic_ops_golang/api/change_log.go
@@ -37,6 +37,10 @@ type ChangeLog struct {
 	LastUpdated tc.Time `json:"lastUpdated" db:"last_updated"`
 }
 
+type Logger interface {
+	ChangeLogMessage(action string, db *sqlx.DB) (string, error)
+}
+
 const (
 	ApiChange = "APICHANGE"
 	Updated   = "Updated"
@@ -45,10 +49,21 @@ const (
 )
 
 func CreateChangeLog(level string, action string, i Identifier, user auth.CurrentUser, db *sqlx.DB) error {
-	query := `INSERT INTO log (level, message, tm_user) VALUES ($1, $2, $3)`
 	id, _ := i.GetID()
 	message := action + " " + i.GetType() + ": " + i.GetAuditName() + " id: " + strconv.Itoa(id)
 	log.Debugf("about to exec ", query, " with ", message)
+	// if the object has its own log message generation, use it
+	if t, ok := i.(Logger); ok {
+		m, err := t.ChangeLogMessage(action, db)
+		if err != nil {
+			log.Errorf("error %++v creating log message for %++v", err, t)
+			// use the default message in this case
+		} else {
+			message = m
+		}
+	}
+
+	query := `INSERT INTO log (level, message, tm_user) VALUES ($1, $2, $3)`
 	_, err := db.Exec(query, level, message, user.ID)
 	if err != nil {
 		log.Errorf("received error: %++v from audit log insertion", err)

-- 
To stop receiving notification emails like this one, please contact
dewrich@apache.org.