"A legalist is anyone who behaves as if they can earn God’s forgiveness through personal performance."
Here is the simple test of legalism. Ask yourself this question:
If the answer is anything other than "He loves me with all of His heart and soul" then we are falling into the trap of believing that God's love for us is earned by our performance rather than freely given (grace).
"[Legalism is] a danger that we’ll never outgrow in this lifetime. The tendency for legalism exists for each of us each and every day—because of the pride and self-righteousness of our indwelling sin."
So, what is the way out?
Legalism is broken at the Cross of Christ where we died and were raised to newness of life in union with Christ:
Through the law I died to the law so that I might live for God. I have been crucified with Christ and I no longer live, but Christ lives in me. The life I live in the body, I live by faith in the Son of God, who loved me and gave himself for me. I do not set aside the grace of God, for if righteousness could be gained through the law, Christ died for nothing! ~ Galatians 2:19-20
This is where we combat our natural tendency to legalism and to this we must return again and again.
(quotes from, CJ Mahaney, The Cross-Centered Life)