Skip to content
This repository was archived by the owner on Apr 25, 2025. It is now read-only.

Commit eff58ec

Browse files
committed
[FAB-6205] Improve log vendoring (populate flogging)
This patch imports fabric flogging into SDK under third_party. Change-Id: I805edd30d2b98eaeea33d87feafdbc09a7c7c4ea Signed-off-by: Troy Ronda <troy.ronda@securekey.com>
1 parent 4bfb47e commit eff58ec

File tree

5 files changed

+335
-11
lines changed

5 files changed

+335
-11
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/bin/bash
2+
#
3+
# Copyright SecureKey Technologies Inc. All Rights Reserved.
4+
#
5+
# SPDX-License-Identifier: Apache-2.0
6+
#
7+
8+
# This script pins the BCCSP package family from Hyperledger Fabric into the SDK
9+
# These files are checked into internal paths.
10+
# Note: This script must be adjusted as upstream makes adjustments
11+
12+
IMPORT_SUBSTS=($IMPORT_SUBSTS)
13+
14+
declare -a PKGS=(
15+
"common/flogging"
16+
)
17+
18+
declare -a FILES=(
19+
"common/flogging/logging.go"
20+
)
21+
22+
echo 'Removing current upstream project from working directory ...'
23+
rm -Rf "${INTERNAL_PATH}/common/flogging"
24+
mkdir -p "${INTERNAL_PATH}/common/flogging"
25+
26+
# Create directory structure for packages
27+
for i in "${PKGS[@]}"
28+
do
29+
mkdir -p $INTERNAL_PATH/${i}
30+
done
31+
32+
# Apply global import patching
33+
echo "Patching import paths on upstream project ..."
34+
for i in "${FILES[@]}"
35+
do
36+
for subst in "${IMPORT_SUBSTS[@]}"
37+
do
38+
sed -i '' -e $subst $TMP_PROJECT_PATH/${i}
39+
done
40+
goimports -w $TMP_PROJECT_PATH/${i}
41+
done
42+
43+
# Copy patched project into internal paths
44+
echo "Copying patched upstream project into working directory ..."
45+
for i in "${FILES[@]}"
46+
do
47+
TARGET_PATH=`dirname $INTERNAL_PATH/${i}`
48+
cp $TMP_PROJECT_PATH/${i} $TARGET_PATH
49+
done

scripts/third_party_pins/fabric/apply_upstream.sh

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ UPSTREAM_BRANCH="master"
1414
SCRIPTS_PATH="scripts/third_party_pins/fabric"
1515
PATCHES_PATH="${SCRIPTS_PATH}/patches"
1616

17-
THIRDPARTY_FABRIC_API_PATH='api/third_party/fabric'
18-
THIRDPARTY_FABRIC_BCCSP_PKG_PATH='pkg/third_party'
17+
THIRDPARTY_FABRIC_PATH='third_party/github.com/hyperledger/fabric'
18+
THIRDPARTY_FABRIC_API_PATH=$THIRDPARTY_FABRIC_PATH
19+
THIRDPARTY_FABRIC_BCCSP_PKG_PATH=$THIRDPARTY_FABRIC_PATH
1920
THIRDPARTY_INTERNAL_FABRIC_PATH='internal/github.com/hyperledger/fabric'
2021

2122
####
@@ -43,36 +44,50 @@ cd $CWD
4344
# fabric client utils
4445
echo "Pinning and patching fabric client utils..."
4546
declare -a CLIENT_UTILS_IMPORT_SUBSTS=(
46-
's/\"github.com\/hyperledger\/fabric\/bccsp/\"github.com\/hyperledger\/fabric-sdk-go\/pkg\/third_party\/bccsp/g'
47-
's/\"github.com\/hyperledger\/fabric\/protos\//\"github.com\/hyperledger\/fabric-sdk-go\/api\/third_party\/fabric\/protos\//g'
47+
's/\"github.com\/hyperledger\/fabric\/common\/flogging/\"github.com\/hyperledger\/fabric-sdk-go\/third_party\/github.com\/hyperledger\/fabric\/common\/flogging/g'
48+
's/\"github.com\/hyperledger\/fabric\/bccsp/\"github.com\/hyperledger\/fabric-sdk-go\/third_party\/github.com\/hyperledger\/fabric\/bccsp/g'
49+
's/\"github.com\/hyperledger\/fabric\/protos\/utils/\"github.com\/hyperledger\/fabric-sdk-go\/internal\/github.com\/hyperledger\/fabric\/protos\/utils/g'
50+
's/\"github.com\/hyperledger\/fabric\/protos\//\"github.com\/hyperledger\/fabric-sdk-go\/third_party\/github.com\/hyperledger\/fabric\/protos\//g'
4851
's/\"github.com\/hyperledger\/fabric\//\"github.com\/hyperledger\/fabric-sdk-go\/internal\/github.com\/hyperledger\/fabric\//g'
4952
)
5053
eval "INTERNAL_PATH=$THIRDPARTY_INTERNAL_FABRIC_PATH TMP_PROJECT_PATH=$TMP_PROJECT_PATH IMPORT_SUBSTS=\"${CLIENT_UTILS_IMPORT_SUBSTS[*]}\" $SCRIPTS_PATH/apply_fabric_client_utils.sh"
5154

55+
# external utils
56+
echo "Pinning and patching fabric external utils ..."
57+
declare -a EXTERNAL_UTILS_IMPORT_SUBSTS=(
58+
's/\"github.com\/hyperledger\/fabric\//\"github.com\/hyperledger\/fabric-sdk-go\/internal\/github.com\/hyperledger\/fabric\//g'
59+
)
60+
eval "INTERNAL_PATH=$THIRDPARTY_FABRIC_PATH TMP_PROJECT_PATH=$TMP_PROJECT_PATH IMPORT_SUBSTS=\"${EXTERNAL_UTILS_IMPORT_SUBSTS[*]}\" $SCRIPTS_PATH/apply_fabric_external_utils.sh"
61+
62+
5263
# bccsp
5364
echo "Pinning and patching bccsp ..."
5465
declare -a BCCSP_IMPORT_SUBSTS=(
55-
's/\"github.com\/hyperledger\/fabric\/bccsp/\"github.com\/hyperledger\/fabric-sdk-go\/pkg\/third_party\/bccsp/g'
66+
's/\"github.com\/hyperledger\/fabric\/common\/flogging/\"github.com\/hyperledger\/fabric-sdk-go\/third_party\/github.com\/hyperledger\/fabric\/common\/flogging/g'
67+
's/\"github.com\/hyperledger\/fabric\/bccsp/\"github.com\/hyperledger\/fabric-sdk-go\/third_party\/github.com\/hyperledger\/fabric\/bccsp/g'
68+
's/\"github.com\/hyperledger\/fabric\//\"github.com\/hyperledger\/fabric-sdk-go\/internal\/github.com\/hyperledger\/fabric\//g'
5669
)
5770
eval "INTERNAL_PATH=$THIRDPARTY_FABRIC_BCCSP_PKG_PATH TMP_PROJECT_PATH=$TMP_PROJECT_PATH IMPORT_SUBSTS=\"${BCCSP_IMPORT_SUBSTS[*]}\" $SCRIPTS_PATH/apply_fabric_bccsp.sh"
5871

5972
# protos
6073
echo "Pinning and patching protos ..."
6174
declare -a PROTOS_IMPORT_SUBSTS=(
62-
's/\"github.com\/hyperledger\/fabric\/bccsp/\"github.com\/hyperledger\/fabric-sdk-go\/pkg\/third_party\/bccsp/g'
63-
's/\"github.com\/hyperledger\/fabric\/protos\//\"github.com\/hyperledger\/fabric-sdk-go\/api\/third_party\/fabric\/protos\//g'
75+
's/\"github.com\/hyperledger\/fabric\/common\/flogging/\"github.com\/hyperledger\/fabric-sdk-go\/third_party\/github.com\/hyperledger\/fabric\/common\/flogging/g'
76+
's/\"github.com\/hyperledger\/fabric\/bccsp/\"github.com\/hyperledger\/fabric-sdk-go\/third_party\/github.com\/hyperledger\/fabric\/bccsp/g'
77+
's/\"github.com\/hyperledger\/fabric\/protos\//\"github.com\/hyperledger\/fabric-sdk-go\/third_party\/github.com\/hyperledger\/fabric\/protos\//g'
6478
's/\"github.com\/hyperledger\/fabric\//\"github.com\/hyperledger\/fabric-sdk-go\/internal\/github.com\/hyperledger\/fabric\//g'
6579
)
6680
eval "INTERNAL_PATH=$THIRDPARTY_FABRIC_API_PATH TMP_PROJECT_PATH=$TMP_PROJECT_PATH IMPORT_SUBSTS=\"${PROTOS_IMPORT_SUBSTS[*]}\" $SCRIPTS_PATH/apply_fabric_protos.sh"
6781

6882
# proto utils
6983
echo "Pinning and patching proto utils..."
7084
declare -a PROTO_UTILS_IMPORT_SUBSTS=(
71-
's/\"github.com\/hyperledger\/fabric\/bccsp/\"github.com\/hyperledger\/fabric-sdk-go\/pkg\/third_party\/bccsp/g'
72-
's/\"github.com\/hyperledger\/fabric\/protos\//\"github.com\/hyperledger\/fabric-sdk-go\/api\/third_party\/fabric\/protos\//g'
85+
's/\"github.com\/hyperledger\/fabric\/common\/flogging/\"github.com\/hyperledger\/fabric-sdk-go\/third_party\/github.com\/hyperledger\/fabric\/common\/flogging/g'
86+
's/\"github.com\/hyperledger\/fabric\/bccsp/\"github.com\/hyperledger\/fabric-sdk-go\/third_party\/github.com\/hyperledger\/fabric\/bccsp/g'
87+
's/\"github.com\/hyperledger\/fabric\/protos\//\"github.com\/hyperledger\/fabric-sdk-go\/third_party\/github.com\/hyperledger\/fabric\/protos\//g'
7388
's/\"github.com\/hyperledger\/fabric\//\"github.com\/hyperledger\/fabric-sdk-go\/internal\/github.com\/hyperledger\/fabric\//g'
7489
)
75-
eval "INTERNAL_PATH=$THIRDPARTY_FABRIC_API_PATH TMP_PROJECT_PATH=$TMP_PROJECT_PATH IMPORT_SUBSTS=\"${PROTO_UTILS_IMPORT_SUBSTS[*]}\" $SCRIPTS_PATH/apply_fabric_proto_utils.sh"
90+
eval "INTERNAL_PATH=$THIRDPARTY_INTERNAL_FABRIC_PATH TMP_PROJECT_PATH=$TMP_PROJECT_PATH IMPORT_SUBSTS=\"${PROTO_UTILS_IMPORT_SUBSTS[*]}\" $SCRIPTS_PATH/apply_fabric_proto_utils.sh"
7691

7792
# Cleanup temporary files from patch application
7893
echo "Removing temporary files ..."
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
From 81c8693fad42fa98ab38c8946fbb79d355e08392 Mon Sep 17 00:00:00 2001
2+
From: Troy Ronda <t.....@securekey.com>
3+
Date: Tue, 19 Sep 2017 11:29:58 -0400
4+
Subject: [PATCH] Update package flogging for vendoring
5+
6+
Copyright SecureKey Technologies Inc. All Rights Reserved.
7+
SPDX-License-Identifier: Apache-2.0
8+
9+
Signed-off-by: Troy Ronda <t.....@securekey.com>
10+
---
11+
common/flogging/logging.go | 1 -
12+
1 file changed, 1 deletion(-)
13+
14+
diff --git a/common/flogging/logging.go b/common/flogging/logging.go
15+
index 841aed7a..f4f0b8d7 100644
16+
--- a/common/flogging/logging.go
17+
+++ b/common/flogging/logging.go
18+
@@ -47,7 +47,6 @@ var (
19+
func init() {
20+
logger = logging.MustGetLogger(pkgLogID)
21+
Reset()
22+
- initgrpclogger()
23+
}
24+
25+
// Reset sets to logging to the defaults defined in this package.
26+
--
27+
2.14.1
28+

test/scripts/unit.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ REPO="github.com/hyperledger/fabric-sdk-go"
1616
PKGS=`go list $REPO... 2> /dev/null | \
1717
grep -v ^$REPO/api/ | \
1818
grep -v ^$REPO/pkg/fabric-ca-client/mocks | grep -v ^$REPO/pkg/fabric-client/mocks | \
19-
grep -v ^$REPO/internal/github.com/ | grep -v ^$REPO/pkg/third_party/ | \
19+
grep -v ^$REPO/internal/github.com/ | grep -v ^$REPO/third_party/ | \
2020
grep -v ^$REPO/vendor/ | grep -v ^$REPO/test/`
2121
echo "Running unit tests..."
2222

Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
/*
2+
Copyright IBM Corp. 2017 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package flogging
18+
19+
import (
20+
"io"
21+
"os"
22+
"regexp"
23+
"strings"
24+
"sync"
25+
26+
"github.com/op/go-logging"
27+
)
28+
29+
const (
30+
pkgLogID = "flogging"
31+
defaultFormat = "%{color}%{time:2006-01-02 15:04:05.000 MST} [%{module}] %{shortfunc} -> %{level:.4s} %{id:03x}%{color:reset} %{message}"
32+
defaultLevel = logging.INFO
33+
)
34+
35+
var (
36+
logger *logging.Logger
37+
38+
defaultOutput *os.File
39+
40+
modules map[string]string // Holds the map of all modules and their respective log level
41+
peerStartModules map[string]string
42+
43+
lock sync.RWMutex
44+
once sync.Once
45+
)
46+
47+
func init() {
48+
logger = logging.MustGetLogger(pkgLogID)
49+
Reset()
50+
}
51+
52+
// Reset sets to logging to the defaults defined in this package.
53+
func Reset() {
54+
modules = make(map[string]string)
55+
lock = sync.RWMutex{}
56+
57+
defaultOutput = os.Stderr
58+
InitBackend(SetFormat(defaultFormat), defaultOutput)
59+
InitFromSpec("")
60+
}
61+
62+
// SetFormat sets the logging format.
63+
func SetFormat(formatSpec string) logging.Formatter {
64+
if formatSpec == "" {
65+
formatSpec = defaultFormat
66+
}
67+
return logging.MustStringFormatter(formatSpec)
68+
}
69+
70+
// InitBackend sets up the logging backend based on
71+
// the provided logging formatter and I/O writer.
72+
func InitBackend(formatter logging.Formatter, output io.Writer) {
73+
backend := logging.NewLogBackend(output, "", 0)
74+
backendFormatter := logging.NewBackendFormatter(backend, formatter)
75+
logging.SetBackend(backendFormatter).SetLevel(defaultLevel, "")
76+
}
77+
78+
// DefaultLevel returns the fallback value for loggers to use if parsing fails.
79+
func DefaultLevel() string {
80+
return defaultLevel.String()
81+
}
82+
83+
// GetModuleLevel gets the current logging level for the specified module.
84+
func GetModuleLevel(module string) string {
85+
// logging.GetLevel() returns the logging level for the module, if defined.
86+
// Otherwise, it returns the default logging level, as set by
87+
// `flogging/logging.go`.
88+
level := logging.GetLevel(module).String()
89+
return level
90+
}
91+
92+
// SetModuleLevel sets the logging level for the modules that match the supplied
93+
// regular expression. Can be used to dynamically change the log level for the
94+
// module.
95+
func SetModuleLevel(moduleRegExp string, level string) (string, error) {
96+
return setModuleLevel(moduleRegExp, level, true, false)
97+
}
98+
99+
func setModuleLevel(moduleRegExp string, level string, isRegExp bool, revert bool) (string, error) {
100+
var re *regexp.Regexp
101+
logLevel, err := logging.LogLevel(level)
102+
if err != nil {
103+
logger.Warningf("Invalid logging level '%s' - ignored", level)
104+
} else {
105+
if !isRegExp || revert {
106+
logging.SetLevel(logLevel, moduleRegExp)
107+
logger.Debugf("Module '%s' logger enabled for log level '%s'", moduleRegExp, level)
108+
} else {
109+
re, err = regexp.Compile(moduleRegExp)
110+
if err != nil {
111+
logger.Warningf("Invalid regular expression: %s", moduleRegExp)
112+
return "", err
113+
}
114+
lock.Lock()
115+
defer lock.Unlock()
116+
for module := range modules {
117+
if re.MatchString(module) {
118+
logging.SetLevel(logging.Level(logLevel), module)
119+
modules[module] = logLevel.String()
120+
logger.Debugf("Module '%s' logger enabled for log level '%s'", module, logLevel)
121+
}
122+
}
123+
}
124+
}
125+
return logLevel.String(), err
126+
}
127+
128+
// MustGetLogger is used in place of `logging.MustGetLogger` to allow us to
129+
// store a map of all modules and submodules that have loggers in the system.
130+
func MustGetLogger(module string) *logging.Logger {
131+
l := logging.MustGetLogger(module)
132+
lock.Lock()
133+
defer lock.Unlock()
134+
modules[module] = GetModuleLevel(module)
135+
return l
136+
}
137+
138+
// InitFromSpec initializes the logging based on the supplied spec. It is
139+
// exposed externally so that consumers of the flogging package may parse their
140+
// own logging specification. The logging specification has the following form:
141+
// [<module>[,<module>...]=]<level>[:[<module>[,<module>...]=]<level>...]
142+
func InitFromSpec(spec string) string {
143+
levelAll := defaultLevel
144+
var err error
145+
146+
if spec != "" {
147+
fields := strings.Split(spec, ":")
148+
for _, field := range fields {
149+
split := strings.Split(field, "=")
150+
switch len(split) {
151+
case 1:
152+
if levelAll, err = logging.LogLevel(field); err != nil {
153+
logger.Warningf("Logging level '%s' not recognized, defaulting to '%s': %s", field, defaultLevel, err)
154+
levelAll = defaultLevel // need to reset cause original value was overwritten
155+
}
156+
case 2:
157+
// <module>[,<module>...]=<level>
158+
levelSingle, err := logging.LogLevel(split[1])
159+
if err != nil {
160+
logger.Warningf("Invalid logging level in '%s' ignored", field)
161+
continue
162+
}
163+
164+
if split[0] == "" {
165+
logger.Warningf("Invalid logging override specification '%s' ignored - no module specified", field)
166+
} else {
167+
modules := strings.Split(split[0], ",")
168+
for _, module := range modules {
169+
logger.Debugf("Setting logging level for module '%s' to '%s'", module, levelSingle)
170+
logging.SetLevel(levelSingle, module)
171+
}
172+
}
173+
default:
174+
logger.Warningf("Invalid logging override '%s' ignored - missing ':'?", field)
175+
}
176+
}
177+
}
178+
179+
logging.SetLevel(levelAll, "") // set the logging level for all modules
180+
181+
// iterate through modules to reload their level in the modules map based on
182+
// the new default level
183+
for k := range modules {
184+
MustGetLogger(k)
185+
}
186+
// register flogging logger in the modules map
187+
MustGetLogger(pkgLogID)
188+
189+
return levelAll.String()
190+
}
191+
192+
// SetPeerStartupModulesMap saves the modules and their log levels.
193+
// this function should only be called at the end of peer startup.
194+
func SetPeerStartupModulesMap() {
195+
lock.Lock()
196+
defer lock.Unlock()
197+
198+
once.Do(func() {
199+
peerStartModules = make(map[string]string)
200+
for k, v := range modules {
201+
peerStartModules[k] = v
202+
}
203+
})
204+
}
205+
206+
// GetPeerStartupLevel returns the peer startup level for the specified module.
207+
// It will return an empty string if the input parameter is empty or the module
208+
// is not found
209+
func GetPeerStartupLevel(module string) string {
210+
if module != "" {
211+
if level, ok := peerStartModules[module]; ok {
212+
return level
213+
}
214+
}
215+
216+
return ""
217+
}
218+
219+
// RevertToPeerStartupLevels reverts the log levels for all modules to the level
220+
// defined at the end of peer startup.
221+
func RevertToPeerStartupLevels() error {
222+
lock.RLock()
223+
defer lock.RUnlock()
224+
for key := range peerStartModules {
225+
_, err := setModuleLevel(key, peerStartModules[key], false, true)
226+
if err != nil {
227+
return err
228+
}
229+
}
230+
logger.Info("Log levels reverted to the levels defined at the end of peer startup")
231+
return nil
232+
}

0 commit comments

Comments
 (0)